l3.libnazgul/src/allocate.c

216 lines
6 KiB
C
Raw Normal View History

2004-02-21 12:59:43 +00:00
#include "libnazgul.h"
2004-02-23 18:50:52 +00:00
#include "ids.h"
2004-02-21 12:59:43 +00:00
/* 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;
int bufferFreeSize;
2004-02-25 08:53:54 +00:00
int i;
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;
msgPoolDataTabSemId ressourceSemId;
2004-02-23 11:02:15 +00:00
sem_t * ressourceSemFd;
2004-02-22 18:09:05 +00:00
int ressourceSemVal;
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;
msgPoolData * mSPoolDataTabAddr;
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-25 08:53:54 +00:00
mSPoolDataTabAddr=msgPoolDataTabOpen(space);
2004-02-25 10:32:41 +00:00
if (mSPoolDataTabAddr==NULL){
NZG_ERROR("msgPoolDataTabOpen",space->poolDataTabId);
goto ERROR;
}
2004-02-25 08:53:54 +00:00
/*
// 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",
(char*)space->poolDataTabId,
2004-02-21 12:59:43 +00:00
strerror( errno ) );
return NULL;
}
2004-02-25 08:53:54 +00:00
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;
}
2004-02-25 08:53:54 +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;
}
nbLockedSem=0;
2004-02-21 12:59:43 +00:00
if ( pool == ANYPOOL){
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 */
2004-02-22 18:09:05 +00:00
if (sem_getvalue(ressourceSemFd, &ressourceSemVal) < 0){
NZG_ERROR("sem_getvalue",ressourceSemId);
return NULL;
2004-02-21 16:12:22 +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] =
(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) {
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;
}
if( sem_close(ressourceSemFd) <0){
NZG_ERROR("sem_getvalue",ressourceSemId);
return NULL;
}
2004-02-21 12:59:43 +00:00
} // if buffSize > taille
} // for
printf("ERRORDETECT outFor\n"); fflush(stdout);
2004-02-22 18:09:05 +00:00
2004-02-21 14:46:24 +00:00
if (!gotRessourceSem) {
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
}
printf("Optimum : %d\n",selectedPoolIndex);
2004-02-21 12:59:43 +00:00
}
}else {
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-25 21:28:11 +00:00
msgPoolSemIdIntern(ressourceSemId,space->externId,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
//TODO:virer la ligne suivante:
sem_getvalue(ressourceSemFd, &ressourceSemVal);
printf("SemWait... %s : %d\n",ressourceSemId,ressourceSemVal);
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-24 11:15:06 +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 */
2004-02-24 11:15:06 +00:00
msgPoolDataTabLock(space);
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){
// aucun buffer libre ?
2004-02-24 11:15:06 +00:00
NZG_ERROR("msgBufferGetFreeIndex","");
goto ERROR;
2004-02-22 11:50:29 +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 16:22:44 +00:00
bufferFreeSize=mSPoolDataTabAddr[selectedPoolIndex].bufferSize;
2004-02-25 22:30:43 +00:00
printf("BufferSize : %d\n", bufferFreeSize);
2004-02-25 08:13:09 +00:00
resultAddr=msgBufferMap(mSPoolDataTabAddr,selectedPoolIndex,bufferFreeIndex);
if (resultAddr==NULL){
NZG_ERROR("msgBufferMap",mSPoolDataTabAddr[selectedPoolIndex].poolId);
goto ERROR;
}
2004-02-23 17:15:20 +00:00
2004-02-22 09:52:22 +00:00
/* - on s'enregistre aupres de ce buffer */
2004-02-25 08:16:12 +00:00
msgBufferAttachProc(mSPoolDataTabAddr,
selectedPoolIndex,
bufferFreeIndex,
resultAddr);
// munmap(mSPoolDataTabAddr,(space->poolNb) * sizeof( msgPoolData ));
/* unmapper le msgPoolDataTab */
msgPoolDataTabClose(space,mSPoolDataTabAddr);
msgPoolDataTabUnlock(space);
2020-03-03 23:00:08 +00:00
printf( "alloc de %p\n", (void *)resultAddr);
2004-02-25 08:16:12 +00:00
return resultAddr;
2020-03-03 23:00:08 +00:00
2004-02-23 16:22:44 +00:00
ERROR:
2004-02-25 08:16:12 +00:00
NZG_ERROR("msgAllocate","error processing");
msgPoolDataTabUnlock(space);
munmap(mSPoolDataTabAddr,(space->poolNb) * sizeof( msgPoolData ));
return NULL;
2004-02-21 12:59:43 +00:00
}