2004-02-21 12:59:43 +00:00
|
|
|
|
#include "libnazgul.h"
|
|
|
|
|
#include "nzg_ids.h"
|
|
|
|
|
|
|
|
|
|
/* TODO: remplacer le bool de msgPoolData par un identifiant
|
|
|
|
|
de semaphore. Le semaphore contient poolNb valeurs (et indique
|
|
|
|
|
donc le nombre de ressources disponnibles).
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
2004-02-22 18:09:05 +00:00
|
|
|
|
/* TODO: prevoir le cas des n<>gatifs dans la taille ... */
|
2004-02-21 12:59:43 +00:00
|
|
|
|
|
|
|
|
|
void * msgAllocate(msgSpace *space,
|
|
|
|
|
int pool,
|
|
|
|
|
int taille,
|
|
|
|
|
int option
|
|
|
|
|
){
|
2004-02-21 16:12:22 +00:00
|
|
|
|
void * resultAddr=NULL;
|
2004-02-21 12:59:43 +00:00
|
|
|
|
int i, mSPoolDataTabFd;
|
2004-02-21 16:12:22 +00:00
|
|
|
|
msgPoolId resultPoolId;
|
2004-02-21 12:59:43 +00:00
|
|
|
|
/* tableau des valeurs des semPoolCoef/pool pour identifier le pool
|
|
|
|
|
* qui sera lib<EFBFBD>r<EFBFBD> le plus rapidement */
|
|
|
|
|
float semPoolCoef[space->poolNb];
|
|
|
|
|
int idxPoolOptimum;
|
2004-02-21 14:46:24 +00:00
|
|
|
|
bool gotRessourceSem;
|
2004-02-23 11:02:15 +00:00
|
|
|
|
sem_t * poolDataTabSemFd;
|
2019-09-17 12:39:18 +00:00
|
|
|
|
msgPoolDataTabSemId ressourceSemId;
|
2004-02-23 11:02:15 +00:00
|
|
|
|
sem_t * ressourceSemFd;
|
2004-02-22 18:09:05 +00:00
|
|
|
|
int ressourceSemVal;
|
2004-02-23 11:14:09 +00:00
|
|
|
|
int nbLockedSem;
|
2004-02-21 12:59:43 +00:00
|
|
|
|
float minPoolCoef;
|
2004-02-22 09:59:56 +00:00
|
|
|
|
int selectedPoolIndex;
|
2004-02-22 11:50:29 +00:00
|
|
|
|
int bufferFreeIndex;
|
2004-02-23 11:15:49 +00:00
|
|
|
|
int mSPoolFd;
|
2004-02-23 11:14:09 +00:00
|
|
|
|
msgPoolData * mSPoolDataTabAddr;
|
|
|
|
|
|
2019-09-17 12:47:35 +00:00
|
|
|
|
|
2004-02-22 09:59:56 +00:00
|
|
|
|
selectedPoolIndex=-1;
|
2004-02-21 16:12:22 +00:00
|
|
|
|
|
2004-02-23 11:02:15 +00:00
|
|
|
|
|
2004-02-23 12:28:31 +00:00
|
|
|
|
|
2004-02-21 12:59:43 +00:00
|
|
|
|
/* TODO: verifier le premier arg du shm_open */
|
2004-02-22 11:50:29 +00:00
|
|
|
|
mSPoolDataTabFd=shm_open(space->poolDataTabId,
|
|
|
|
|
O_RDWR,
|
|
|
|
|
MSGSPACE_DEFAULT_MODE);
|
2004-02-21 12:59:43 +00:00
|
|
|
|
if (mSPoolDataTabFd == -1 ) {
|
|
|
|
|
fprintf( stderr, "Allocate %s failed: %s\n",
|
2019-09-17 12:39:18 +00:00
|
|
|
|
(char*)space->poolDataTabId,
|
2004-02-21 12:59:43 +00:00
|
|
|
|
strerror( errno ) );
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-22 11:50:29 +00:00
|
|
|
|
mSPoolDataTabAddr = mmap( 0, (space->poolNb) * sizeof( msgPoolData ),
|
2004-02-21 12:59:43 +00:00
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
|
MAP_SHARED, mSPoolDataTabFd, 0 );
|
|
|
|
|
|
2004-02-22 11:50:29 +00:00
|
|
|
|
if( mSPoolDataTabAddr == MAP_FAILED) {
|
2004-02-21 12:59:43 +00:00
|
|
|
|
fprintf( stderr, "mmap failed: %s\n",
|
|
|
|
|
strerror( errno ) );
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 12:47:35 +00:00
|
|
|
|
gotRessourceSem=false;
|
2004-02-21 12:59:43 +00:00
|
|
|
|
|
|
|
|
|
/* initialisation des coefs */
|
|
|
|
|
for (i=0;i<(space->poolNb);i++){
|
|
|
|
|
semPoolCoef[i]=-1;
|
|
|
|
|
}
|
2019-09-17 12:47:35 +00:00
|
|
|
|
|
2004-02-23 11:14:09 +00:00
|
|
|
|
nbLockedSem=0;
|
2004-02-21 12:59:43 +00:00
|
|
|
|
if ( pool == ANYPOOL){
|
2019-09-17 12:47:35 +00:00
|
|
|
|
fprintf(stderr,"[ ALLOCATION ANYPOOL : %d ]\n",(int)getpid());
|
2004-02-21 12:59:43 +00:00
|
|
|
|
// choisir le pool au hasard (ou presque)
|
|
|
|
|
for(i=0; i<(space->poolNb); i++) {
|
2004-02-23 11:02:15 +00:00
|
|
|
|
printf("- boucle %d\n",i); fflush(stdout);
|
2004-02-22 11:50:29 +00:00
|
|
|
|
if(mSPoolDataTabAddr[i].bufferSize >= taille) {
|
2004-02-23 11:02:15 +00:00
|
|
|
|
printf("( buffSize > taille )\n"); fflush(stdout);
|
2004-02-21 12:59:43 +00:00
|
|
|
|
/* choisir le numero du semaphore
|
|
|
|
|
en fonction du nombre de lock poses / nombre de buffer */
|
2004-02-21 16:12:22 +00:00
|
|
|
|
msgPoolSemIdIntern(ressourceSemId,space->id,i);
|
|
|
|
|
ressourceSemFd = sem_open(ressourceSemId,O_CREAT,SEM_DEFAULT_MODE,0);
|
|
|
|
|
if (ressourceSemFd == SEM_FAILED){
|
2004-02-22 15:29:36 +00:00
|
|
|
|
NZG_ERROR("sem_open",ressourceSemId);
|
|
|
|
|
return NULL;
|
2004-02-21 16:12:22 +00:00
|
|
|
|
}
|
2004-02-21 12:59:43 +00:00
|
|
|
|
/* on remplit le tableau avec les valeurs des semaphores */
|
2019-09-17 12:47:35 +00:00
|
|
|
|
|
2004-02-22 18:09:05 +00:00
|
|
|
|
if (sem_getvalue(ressourceSemFd, &ressourceSemVal) < 0){
|
2019-09-17 12:47:35 +00:00
|
|
|
|
NZG_ERROR("sem_getvalue",ressourceSemId);
|
|
|
|
|
return NULL;
|
2004-02-21 16:12:22 +00:00
|
|
|
|
}
|
2019-09-17 12:47:35 +00:00
|
|
|
|
printf("RESSOURCESEMVAL %d\n",ressourceSemVal);
|
|
|
|
|
if (ressourceSemVal <= 0){
|
|
|
|
|
printf("resVal < 0 : %d\n",ressourceSemVal);
|
2004-02-21 16:12:22 +00:00
|
|
|
|
/* il y a ressourceSemVal processus qui attendent d<>ja... */
|
|
|
|
|
semPoolCoef[nbLockedSem] =
|
2004-02-23 10:01:26 +00:00
|
|
|
|
(float) ((1-ressourceSemVal) / mSPoolDataTabAddr[i].bufferNb);
|
2004-02-21 12:59:43 +00:00
|
|
|
|
nbLockedSem++;
|
|
|
|
|
}
|
2004-02-22 18:09:05 +00:00
|
|
|
|
if(sem_trywait(ressourceSemFd)==0) {
|
2019-09-17 12:47:35 +00:00
|
|
|
|
printf("got try_wait\n");
|
2004-02-21 12:59:43 +00:00
|
|
|
|
/* choisir la 1ere pool de taille plus grande
|
|
|
|
|
* libre si possible */
|
2004-02-21 14:46:24 +00:00
|
|
|
|
gotRessourceSem=true;
|
2004-02-22 09:59:56 +00:00
|
|
|
|
selectedPoolIndex=i;
|
2004-02-21 12:59:43 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2019-09-17 12:47:35 +00:00
|
|
|
|
if( sem_close(ressourceSemFd) <0){
|
|
|
|
|
NZG_ERROR("sem_getvalue",ressourceSemId);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2004-02-21 12:59:43 +00:00
|
|
|
|
} // if buffSize > taille
|
|
|
|
|
} // for
|
|
|
|
|
|
2019-09-17 12:47:35 +00:00
|
|
|
|
printf("ERRORDETECT outFor\n"); fflush(stdout);
|
2004-02-22 18:09:05 +00:00
|
|
|
|
|
2004-02-21 14:46:24 +00:00
|
|
|
|
if (!gotRessourceSem) {
|
2019-09-17 12:47:35 +00:00
|
|
|
|
printf("Calcul du meilleur en cas de liberation\n");
|
2004-02-21 12:59:43 +00:00
|
|
|
|
minPoolCoef= semPoolCoef[0];
|
|
|
|
|
idxPoolOptimum = 0;
|
|
|
|
|
/* on cherche le pool avec le moins de lock poses / nbre de buffer
|
|
|
|
|
* le num<EFBFBD>ro du pool est stock<EFBFBD> dans idxPoolOptimum */
|
|
|
|
|
for(i=0; i<nbLockedSem; i++) {
|
2004-02-22 22:11:59 +00:00
|
|
|
|
printf("Coef %d : %d\n",i,(int)semPoolCoef[i]);
|
2004-02-21 12:59:43 +00:00
|
|
|
|
if ((semPoolCoef[i] != -1)
|
|
|
|
|
&& (semPoolCoef[i] < minPoolCoef)) {
|
|
|
|
|
minPoolCoef = semPoolCoef[i];
|
|
|
|
|
idxPoolOptimum = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (minPoolCoef == -1){
|
|
|
|
|
/* il n'y a aucune pool dont la taille satisfait la demande */
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
2004-02-22 09:59:56 +00:00
|
|
|
|
selectedPoolIndex=idxPoolOptimum;
|
2004-02-21 12:59:43 +00:00
|
|
|
|
}
|
2019-09-17 12:47:35 +00:00
|
|
|
|
printf("Optimum : %d\n",selectedPoolIndex);
|
2004-02-21 12:59:43 +00:00
|
|
|
|
}
|
|
|
|
|
}else {
|
2019-09-17 12:47:35 +00:00
|
|
|
|
fprintf(stderr,"[ ALLOCATION : %d ]\n",(int)getpid());
|
2004-02-22 09:59:56 +00:00
|
|
|
|
selectedPoolIndex=pool;
|
2004-02-21 12:59:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-02-21 16:12:22 +00:00
|
|
|
|
if (!gotRessourceSem){
|
2004-02-22 09:59:56 +00:00
|
|
|
|
msgPoolSemIdIntern(ressourceSemId,space->id,selectedPoolIndex);
|
2004-02-21 16:12:22 +00:00
|
|
|
|
ressourceSemFd=sem_open(ressourceSemId,O_CREAT,SEM_DEFAULT_MODE,0);
|
2004-02-21 16:49:52 +00:00
|
|
|
|
if(ressourceSemFd==SEM_FAILED){
|
2004-02-22 15:29:36 +00:00
|
|
|
|
NZG_ERROR("sem_open",ressourceSemId);
|
2004-02-21 16:49:52 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2004-02-21 16:12:22 +00:00
|
|
|
|
|
2004-02-23 10:01:26 +00:00
|
|
|
|
//TODO:virer la ligne suivante:
|
|
|
|
|
sem_getvalue(ressourceSemFd, &ressourceSemVal);
|
|
|
|
|
|
|
|
|
|
printf("SemWait... %s : %d\n",ressourceSemId,ressourceSemVal);
|
2019-09-17 12:47:35 +00:00
|
|
|
|
if (sem_wait(ressourceSemFd) < 0){
|
|
|
|
|
NZG_ERROR("sem_wait",ressourceSemId);
|
|
|
|
|
sem_close(ressourceSemFd);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
printf("Passed %s\n",ressourceSemId);
|
|
|
|
|
}
|
2004-02-21 16:12:22 +00:00
|
|
|
|
|
2004-02-21 16:49:52 +00:00
|
|
|
|
/* on a acqui un semaphore pour la ressouce */
|
|
|
|
|
/* on acquiert le droit de modifier les infos sur la ressource */
|
2004-02-23 12:28:31 +00:00
|
|
|
|
/* on protege le tableau des associations */
|
|
|
|
|
poolDataTabSemFd=sem_open(space->poolDataTabSemId,
|
|
|
|
|
O_CREAT,SEM_DEFAULT_MODE,1);
|
|
|
|
|
if (poolDataTabSemFd == SEM_FAILED){
|
|
|
|
|
NZG_ERROR("sem_open : ouverture de la ressource",
|
|
|
|
|
space->poolDataTabSemId);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
sem_wait(poolDataTabSemFd);
|
2004-02-23 10:01:26 +00:00
|
|
|
|
|
2004-02-21 14:46:24 +00:00
|
|
|
|
/* on modifie maintenant les donn<6E>es */
|
2004-02-22 09:52:22 +00:00
|
|
|
|
/* - on r<>cupere l'index du premier buffer libre */
|
2004-02-22 11:50:29 +00:00
|
|
|
|
bufferFreeIndex = msgBufferGetFreeIndex(mSPoolDataTabAddr,selectedPoolIndex);
|
|
|
|
|
if (bufferFreeIndex < 0){
|
|
|
|
|
sem_close(poolDataTabSemFd);
|
|
|
|
|
// aucun buffer libre ?
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2004-02-23 10:30:02 +00:00
|
|
|
|
printf("Buffer selected : %d,%d\n",selectedPoolIndex,bufferFreeIndex);
|
2004-02-23 11:02:15 +00:00
|
|
|
|
|
|
|
|
|
/* mapper le buffer libre dans l'esp addr du proc */
|
|
|
|
|
strcpy(resultPoolId,mSPoolDataTabAddr[selectedPoolIndex].poolId);
|
|
|
|
|
|
2004-02-23 12:28:31 +00:00
|
|
|
|
|
2004-02-23 11:15:49 +00:00
|
|
|
|
mSPoolFd=shm_open(resultPoolId,O_RDWR,MSGSPACE_DEFAULT_MODE);
|
2004-02-23 12:28:31 +00:00
|
|
|
|
|
2004-02-23 13:48:56 +00:00
|
|
|
|
|
|
|
|
|
int bufferFreeSize=mSPoolDataTabAddr[selectedPoolIndex].bufferSize;
|
2004-02-23 11:02:15 +00:00
|
|
|
|
resultAddr = mmap( 0,
|
2004-02-23 13:48:56 +00:00
|
|
|
|
bufferFreeSize,
|
2004-02-23 11:02:15 +00:00
|
|
|
|
PROT_READ | PROT_WRITE,
|
2004-02-23 13:48:56 +00:00
|
|
|
|
MAP_SHARED, mSPoolFd, bufferFreeSize*bufferFreeIndex );
|
2004-02-23 12:28:31 +00:00
|
|
|
|
|
2004-02-22 09:52:22 +00:00
|
|
|
|
//TODO:
|
|
|
|
|
/* - on s'enregistre aupres de ce buffer */
|
2004-02-23 12:28:31 +00:00
|
|
|
|
msgBufferAttachProc(mSPoolDataTabAddr,
|
|
|
|
|
selectedPoolIndex,
|
|
|
|
|
bufferFreeIndex,
|
|
|
|
|
resultAddr);
|
|
|
|
|
close(mSPoolFd);
|
2004-02-21 16:12:22 +00:00
|
|
|
|
|
2004-02-23 11:02:15 +00:00
|
|
|
|
sem_post(poolDataTabSemFd);
|
2019-09-17 12:39:18 +00:00
|
|
|
|
sem_close(poolDataTabSemFd);
|
2004-02-21 16:49:52 +00:00
|
|
|
|
|
2004-02-23 13:48:56 +00:00
|
|
|
|
/* unmapper le msgPoolDataTab */
|
|
|
|
|
munmap(mSPoolDataTabAddr,(space->poolNb) * sizeof( msgPoolData ));
|
2004-02-21 12:59:43 +00:00
|
|
|
|
|
|
|
|
|
return resultAddr;
|
|
|
|
|
}
|