l3.libnazgul/src/nzg_allocate.c

209 lines
5.7 KiB
C
Raw Normal View History

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;
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;
2004-02-23 11:15:49 +00:00
int mSPoolFd;
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
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-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",
(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;
}
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-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
//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-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 */
//sem_wait(poolDataTabSemFd);
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;
}
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 11:15:49 +00:00
mSPoolFd=shm_open(resultPoolId,O_RDWR,MSGSPACE_DEFAULT_MODE);
2004-02-23 11:02:15 +00:00
resultAddr = mmap( 0,
mSPoolDataTabAddr[selectedPoolIndex].bufferSize,
PROT_READ | PROT_WRITE,
MAP_SHARED, mSPoolFd, 0 );
2004-02-22 09:52:22 +00:00
//TODO:
/* - on s'enregistre aupres de ce buffer */
2004-02-23 11:02:15 +00:00
//msgBufferAttachProc(mSPoolDataTabFd,selectedPoolIndex,resultAddr);
2004-02-21 16:12:22 +00:00
2004-02-23 11:02:15 +00:00
sem_post(poolDataTabSemFd);
sem_close(poolDataTabSemFd);
2004-02-21 16:49:52 +00:00
2004-02-21 12:59:43 +00:00
/* TODO: unmapper le msgPoolDataTab */
return resultAddr;
}