Example #1
0
bool JudgeSys::initJudgeSys()
{
	bool b_ret = true;
	b_ret = b_ret && initMemPool();
	b_ret = b_ret && initJudgePool();
	ADD_SERVER_LOG("%s -- %s", __FUNCTION__, b_ret?"OK":"FAULT");
	return true;
}
  void increaseMemPoolSize(unsigned int poolId)
  {
    unsigned int requiredMemSize = memBlkGrowthNum[poolId] * (memBlockSize[poolId] + MEM_BLOCK_HDR);
    
    /* See if there is enough heap reserve memory */
    if (requiredMemSize <= reservedHeapSize)
    {
      /* We're in luck; there's enough space */
      pMemPools[poolId] = initMemPool(poolId, memBlockSize[poolId] + MEM_BLOCK_HDR, memBlkGrowthNum[poolId], &pReservedHeapMem);
      
      memBlockNum[poolId] += memBlkGrowthNum[poolId];
      
      reservedHeapSize -= requiredMemSize;
      usedHeapSize += requiredMemSize;
      
#ifdef MEM_MGR_STATS
      memBlkGrowths[poolId]++;
#endif /* MEM_MGR_STATS */
    }
#ifdef ALLOW_ADDITIONAL_SYS_MEM_ALLOCS
    else
    {
      /* There's not enough memory in the heap reserve, so request it from the system */
      char* pStartAddress = (char*)malloc(requiredMemSize);
      
      if (0 != pStartAddress)
      {
        /* The system has allocated some memory, so let's make some more blocks */
        pMemPools[poolId] = initMemPool(poolId, memBlockSize[poolId] + MEM_BLOCK_HDR, memBlkGrowthNum[poolId], &pStartAddress);
        
        memBlockNum[poolId] += memBlkGrowthNum[poolId];
        
        totalSystemAllocMem += requiredMemSize;
        numOfSystemAllocs++;
        
#ifdef MEM_MGR_STATS
        memBlkGrowths[poolId]++;
#endif /* MEM_MGR_STATS */
      }
    }
#endif /* ALLOW_ADDITIONAL_SYS_MEM_ALLOCS */
  }
  void initAllMemPools(void)
  {
    char *availableMemStartAddress;
    
    if (0 == memPoolsInitialised)
    {
      int ii;
      
      /* Calculate the required heap size */
      for (ii = 0; ii < NUM_OF_POOLS; ii++)
      {
        usedHeapSize += (memBlockSize[ii] + MEM_BLOCK_HDR) * memBlockNum[ii];
      }
      
      if (initialHeapSize < usedHeapSize)
      {
        /* Insuffucient heap memory; abort initialisation */
        return;
      }
      
      
#if defined(STATIC_MEMORY_POOL)
      /* pHead has already been allocated, statically.  Don't need to do anything. */
#elif defined(RTXC_PARTITION_MEMORY)
      /* Grab the one and only block in HEAP_MAP. */
      pHeap = KS_alloc(HEAP_MAP);
      /* MEM_SIZE has better equal the size of HEAP_MAP's block. */
      PORT_ASSERT(MEM_SIZE == KS_inqmap(HEAP_MAP));
#else
      /* Use the system malloc for heap allocation. */
      
      pHeap = (char*)malloc(initialHeapSize);
#endif
      if (0 == pHeap)
      {
        /* Unable to get memory for heap; abort initialisation */
        return;
      }
      
      totalSystemAllocMem = initialHeapSize;
      numOfSystemAllocs++;
      reservedHeapSize = initialHeapSize - usedHeapSize;
      
      /* Initialise each memory pool */
      availableMemStartAddress = pHeap;
      
      for (ii = 0; ii < NUM_OF_POOLS; ii++)
      {
        pMemPools[ii] = 0;
        
        if (0 != memBlockNum[ii])
        {
          pMemPools[ii] = initMemPool(ii, memBlockSize[ii] + MEM_BLOCK_HDR, memBlockNum[ii], &availableMemStartAddress);
        }
      }
      
      pReservedHeapMem = availableMemStartAddress;
      
      memPoolsInitialised = 1;
    }
  }
Example #4
0
int main(int argc, char *argv[])
{
    FILE *fp;
    int i = 0;
    char line[MAX_LAST_NAME_SIZE];
    struct timespec start, end;
    double cpu_time1, cpu_time2;

    /* check file opening */
    fp = fopen(DICT_FILE, "r");
    if (fp == NULL) {
        printf("cannot open the file\n");
        return -1;
    }

    /* setting mempool and build the entry*/
#if defined(OPT)
    initMemPool();
    printf("size of entry : %lu bytes\n", sizeof(entry));
#else
    entry *pHead, *e;
    pHead = (entry *) malloc(sizeof(entry));
    printf("size of entry : %lu bytes\n", sizeof(entry));
    e = pHead;
    e->pNext = NULL;
#endif

#if defined(OPT)
#if defined(__GNUC__)
    __builtin___clear_cache((char *) HashTable, (char *) HashTable + sizeof(entry));
#endif
#else
#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
#endif

#if defined(OPT)
#if defined(SLOT)
    int hash_slot[HASH_TABLE_SIZE] = {0};
#endif
    clock_gettime(CLOCK_REALTIME, &start);
    while (fgets(line, sizeof(line), fp)) {
        while (line[i] != '\0')
            i++;
        line[i - 1] = '\0';
        i = 0;
#if defined(SLOT)
        hash_slot[hash(line)]++;
#endif
        if(append(line, HashTable)==NULL)
            puts("apped failed, the lastName is not valid.");
    }
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);


    /* close file as soon as possible */
    fclose(fp);

    /* the givn last name to find */
    char input[MAX_LAST_NAME_SIZE] = "zyxel";
    assert(findName(input, HashTable) &&
           "Did you implement findName() in " IMPL "?");
    assert(0 == strcmp(findName(input, HashTable)->lastName, "zyxel"));
#else

    clock_gettime(CLOCK_REALTIME, &start);
    while (fgets(line, sizeof(line), fp)) {
        while (line[i] != '\0')
            i++;
        line[i - 1] = '\0';
        i = 0;
        if((e = append(line, e))==NULL)
            puts("apped failed, the lastName is not valid.");
    }
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);

    /* close file as soon as possible */
    fclose(fp);

    e = pHead;

    /* the givn last name to find */
    char input[MAX_LAST_NAME_SIZE] = "zyxel";
    e = pHead;

    assert(findName(input, e) &&
           "Did you implement findName() in " IMPL "?");
    assert(0 == strcmp(findName(input, e)->lastName, "zyxel"));
#endif

#if defined(OPT)
#if defined(__GNUC__)
    __builtin___clear_cache((char *) HashTable, (char *) HashTable + sizeof(entry));
#endif
#else
#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
#endif
    /* compute the execution time */
#if defined(OPT)
    clock_gettime(CLOCK_REALTIME, &start);
    findName(input, HashTable);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time2 = diff_in_second(start, end);
#else
    clock_gettime(CLOCK_REALTIME, &start);
    findName(input, e);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time2 = diff_in_second(start, end);
#endif
    FILE *output;
#if defined(OPT)
    output = fopen("opt.txt", "a");
#if defined(SLOT)
    FILE *output2;
    output2 = fopen("slot.txt", "a");
    for(int i = 0; i < HASH_TABLE_SIZE; i++) {
        fprintf(output2, "%d %d\n", i, hash_slot[i]);
    }
    fclose(output2);
#endif

#else
    output = fopen("orig.txt", "a");
#endif
    fprintf(output, "append() findName() %lf %.9lf\n", cpu_time1, cpu_time2);
    fclose(output);

    printf("execution time of append() : %lf sec\n", cpu_time1);
    printf("execution time of findName() : %.9lf sec\n", cpu_time2);

#if defined(OPT)
    mempoolfree();
#else
    if (pHead->pNext) free(pHead->pNext);
    free(pHead);
#endif
    return 0;
}