Beispiel #1
0
void main(int argc, char** argv)
{ initData();

  clock_gettime(CLOCK_REALTIME, &start);
  int i;
  for(i=0; i < ITERATIONS; i++)
  { add_arrays(); }
  clock_gettime(CLOCK_REALTIME, &finish);

  printf("%i\n", a[500]);
  fprintf (stderr, "Total time: %03li\n", xelapsed (finish, start));
}
Beispiel #2
0
/* Measure the time for NUMBER fork creations.  */
void measure_forks (unsigned number)
{ struct timespec start, stop, finish;
  unsigned i = 0;

  //store start time, handle error on failure
  if(clock_gettime(CLOCK_REALTIME, &start) == -1) { perror("Bad time.\n"); exit(EXIT_SUCCESS);}
  pid_t* pids = malloc (number*sizeof(pid_t));
  for (i = 0;i < number; i++)
  { pids[i] = fork();
    //in the child
    if(pids[i] == 0)
    { dummy();
      //free the pointer memory that has been forked
      free(pids);
      exit(EXIT_SUCCESS);
    }
    else if(pids[i] < 0)
    { perror("Bad fork.\n"); exit(EXIT_SUCCESS);}
  }
  //store the stop time
  if(clock_gettime(CLOCK_REALTIME, &stop) == -1) { perror("Bad time.\n"); exit(EXIT_SUCCESS);}

  for (i = 0; i < number; i++)
  //wait for every child for die
  { wait(&pids[i]);
  }
  //get the time needed for every child to die
  if(clock_gettime(CLOCK_REALTIME, &finish) == -1) { perror("Bad time.\n"); exit(EXIT_SUCCESS);}

  //print out the time
  printf ("process: num=%03u, fork=%03li, wait=%03li, total=%03li\n",
          number, xelapsed (stop, start), xelapsed (finish, stop),
          xelapsed (finish, start));
  //free memory used for array of process ids
  free(pids);
}
int
main ()
{
    #define N (100 * 1000)

    /* Array of chunks.  */
    struct chunk mem_pool[N];

    /* One GB of bytes. */
    const size_t one_gb = 1024 * 1024 * 1024;
    size_t allocated_memory = 0;

    /* Limit our allicated memory to 1GB - 1024.  */
    size_t free_memory = one_gb - 1024;

    size_t i = 0;

    struct timespec start, finish;
    int success = 0;

    /* Make sure we limit ourselves to 1 GB.  */
    struct rlimit new_limit = {one_gb, one_gb};



    /* Make sure that the structure is being filled with zeroes.  */
    memset (mem_pool, 0, N*sizeof (struct chunk));


    if (-1 == setrlimit (RLIMIT_AS, &new_limit))
        err (EXIT_FAILURE, "setrlimit failed");
    if (-1 == setrlimit (RLIMIT_DATA, &new_limit))
        err (EXIT_FAILURE, "setrlimit failed");

/* Free the cell IDX in the POOL, allocte memory
   of size N.  If malloc failed, goto cleanup.
   Otherwise adjust FREE_MEMORY and ALLOCATAED_MEMORY  */
#define xmalloc(pool, idx, n)                   \
    do {                                        \
        __free (&pool[idx]);                    \
        pool[idx] = __malloc (n);               \
        if (pool[idx].ptr == NULL)              \
            goto cleanup;                       \
        allocated_memory += n;                  \
        free_memory -= n;                       \
    } while (0)

/* Check if IDX cell in POOL contains non-zero
   memory pointer.  If so, free it and update
   ALLOCATED_MEMORY and FREE_MEMORY.  */
#define xfree(pool, idx)                        \
    do {                                        \
        if (pool[idx].ptr) {                    \
            __free (&pool[idx]);                \
            pool[idx].ptr = NULL;               \
            allocated_memory -= pool[idx].sz;   \
            free_memory += pool[idx].sz;        \
        }                                       \
    } while (0)

    /* Start clock.  */
    clock_gettime(CLOCK_REALTIME, &start);

    /* Allocate N chunks 1 byte each.  */
    for (i = 0; i < N; i++) {
        if (free_memory < 1)
            break;

        xmalloc (mem_pool, i, 1);
    }

    /* Free all the N allocated chunks.  */
    for (i = 0; i < N; i++)
        xfree (mem_pool, i);

    /* Allocate a 1/4 GB chunks three times.  */
    xmalloc (mem_pool, 0, one_gb/4);
    xmalloc (mem_pool, 1, one_gb/4);
    xmalloc (mem_pool, 2, one_gb/4);

    /* Mark success of the test.  */
    success = 1;

cleanup:
    /* Free all the non-free memory in the MEM_POOL.  */
    for (i = 0; i < N; i++)
        xfree (mem_pool, i);

    /* Stop clock.  */
    clock_gettime(CLOCK_REALTIME, &finish);

    if (success)
        fprintf (stderr, "Total time: %03li\n", xelapsed (finish, start));
    else
        fprintf (stderr, "The test didn't finish successfully\n");

    return success ? EXIT_SUCCESS : EXIT_FAILURE;
}