/*! * \ingroup cache_interface * * Cette fonction crée et initialise le cache : * - Allocation du cache lui-même * - Ouverture du fichier * - Initialisation des champs du cache. * - Allocation des entêtes de blocs et des blocs eux-memes * * \param file nom du fichier * \param nblocks nombre de blocs dans le cache * \param nrecords nombre d'enregistrements par bloc * \param recordsz taille d'un enregistrement * \param nderef période de déréférençage (pour NUR) * \return un pointeur sur le nouveau cache ou le pointeur nul en cas d'erreur */ struct Cache *Cache_Create(const char *file, unsigned nblocks, unsigned nrecords, size_t recordsz, unsigned nderef) { int ib; /* Allocation de la structure du cache */ struct Cache *pcache = (struct Cache *)malloc(sizeof(struct Cache)); /* Sauvegarde du nom de fichier */ pcache->file = (char *)malloc(strlen(file) + 1); strcpy(pcache->file, file); /* Ouverture du fichier en mode "update" : * On verifie d'abord que le fichier existe ("r+"). * Sinon on le cree ("w+"). */ if ((pcache->fp = fopen(file, "r+")) == NULL) if ((pcache->fp = fopen(file, "w+")) == NULL) return CACHE_KO; /* Initialisation des valeurs de dimensionnement */ if (nblocks > 0) pcache->nblocks = nblocks; else return 0; if (nrecords > 0) pcache->nrecords = nrecords; else return 0; if (recordsz > 0) pcache->recordsz = recordsz; else return 0; if (recordsz > 0) pcache->nderef = nderef; else return 0; pcache->blocksz = nrecords*recordsz; /* Allocation des entetes de bloc, et des blocs eux-memes */ pcache->headers = malloc(nblocks*sizeof(struct Cache_Block_Header)); for (ib = 0; ib < nblocks; ib++) { pcache->headers[ib].data = (char *)malloc(pcache->blocksz); pcache->headers[ib].ibcache = ib; pcache->headers[ib].flags = 0; } /* Initialisation du pointeur sur le premier bloc libre, cad ici le premier * bloc */ pcache->pfree = pcache->headers; /* Mise à 0 des données d'instrumentation */ (void)Cache_Get_Instrument(pcache); /* Initialisation de la stratégie */ pcache->pstrategy = Strategy_Create(pcache); /* On rend ce que l'on vient de créer et d'initialiser */ return pcache; }
/* Recupération des données d'instrumentation * ------------------------------------------ */ static void Print_Instrument(struct Cache *pcache, const char *msg) { struct Cache_Instrument *pinstr = Cache_Get_Instrument(pcache); if (Short_Output) { printf("hits %.1f\n", ((double)pinstr->n_hits)/(pinstr->n_reads + pinstr->n_writes)*100); } else { printf("\n%s : \n", msg == NULL ? "" : msg); printf("\t%d lectures %d écritures %d succès (%.1f %%)\n", pinstr->n_reads, pinstr->n_writes, pinstr->n_hits, ((double)pinstr->n_hits)/(pinstr->n_reads + pinstr->n_writes)*100); printf("\t%d syncs %d déréférençages\n", pinstr->n_syncs, pinstr->n_deref); } }