The benchmark:<br>/*<br> * test.c - Simulate workloads that load the CPU differently<br> *<br> * This program is free software; you can redistribute it and/or<br> * modify it under the terms of the GNU General Public License as<br>
 * published by the Free Software Foundation; version 2 of the License.<br> *<br> * This program is distributed in the hope that it will be useful,<br> * but WITHOUT ANY WARRANTY; without even the implied warranty of<br> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the<br>
 * GNU General Public License for more details.<br> *<br> * You should have received a copy of the GNU General Public License<br> * along with this program; if not, write to the Free Software<br> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307<br>
 * USA<br> */<br><br>/*<br> * This workload spawns threads which request for allocation of a<br> * memory chunk,write to it and free it.The duty cycle of these threads<br> * can be varied.The idea is to simulate tasks which load the cpu<br>
 * to different extents.<br> */<br><br>#include <stdio.h><br>#include <unistd.h><br>#include <stdlib.h><br>#include <sys/mman.h><br>#include <pthread.h><br>#include <string.h><br>#include <time.h><br>
#include <sys/time.h><br>#include <sys/resource.h><br>#include "malloc.h"<br><br>/* Variable entities */<br>static unsigned int seconds;<br>static unsigned int threads;<br>static unsigned int mem_chunk_size;<br>
static unsigned int sleep_at;<br>static unsigned int sleep_interval;<br><br>typedef size_t mem_slot_t;/* 8 bytes */<br>static unsigned int slot_size = sizeof(mem_slot_t);<br><br>/* Other parameters */<br>static volatile int start;<br>
static time_t start_time;<br>static unsigned int records_read;<br>pthread_mutex_t records_count_lock = PTHREAD_MUTEX_INITIALIZER;<br><br><br>static unsigned int write_to_mem(void)<br>{<br>    int i, j;<br>    mem_slot_t *scratch_pad, *temp;<br>
    mem_chunk_size = slot_size * 256;<br>    mem_slot_t *end;<br><br>    /* The below two parameters ensure that it is 10% workload<br>     * with a duty cycle of 10ms.The number of records read in<br>     * 1s without sleep was observed and appropriately calculated<br>
     * for 1ms.This number turned out to be 1228.<br>     */<br>    sleep_at = 1228; /* sleep for every 1228 records */<br>    sleep_interval = 9000; /* sleep for 9 ms */<br><br>    for (i=0; start == 1; i++)<br>    {<br>
        scratch_pad = (mem_slot_t *)malloc(mem_chunk_size);<br>        if (scratch_pad == NULL) {<br>            fprintf(stderr,"Could not allocate memory\n");<br>            exit(1);<br>        }<br>        end = scratch_pad + (mem_chunk_size / slot_size);<br>
        for (temp = scratch_pad, j=0; temp < end; temp++, j++)<br>            *temp = (mem_slot_t)j;<br><br>        free(scratch_pad);<br><br>        if (sleep_at && !(i % sleep_at))<br>            usleep(sleep_interval);<br>
    }<br>    return (i);<br>}<br><br>static void *<br>thread_run(void *arg)<br>{<br><br>    unsigned int records_local;<br><br>    /* Wait for the start signal */<br><br>    while (start == 0);<br><br>    records_local = write_to_mem();<br>
<br>    pthread_mutex_lock(&records_count_lock);<br>    records_read += records_local;<br>    pthread_mutex_unlock(&records_count_lock);<br><br>    return NULL;<br>}<br><br>static void start_threads()<br>{<br>    double diff_time;<br>
    unsigned int i;<br>    int err;<br>    threads = 8;<br>    seconds = 10;<br><br>    pthread_t thread_array[threads];<br>    for (i = 0; i < threads; i++) {<br>        err = pthread_create(&thread_array[i], NULL, thread_run, NULL);<br>
        if (err) {<br>            fprintf(stderr, "Error creating thread %d\n", i);<br>            exit(1);<br>        }<br>    }<br>    start_time = time(NULL);<br>    start = 1;<br>    sleep(seconds);<br>    start = 0;<br>
    diff_time = difftime(time(NULL), start_time);<br><br>    for (i = 0; i < threads; i++) {<br>        err = pthread_join(thread_array[i], NULL);<br>        if (err) {<br>            fprintf(stderr, "Error joining thread %d\n", i);<br>
            exit(1);<br>        }<br>    }<br>     printf("%u records/s\n",<br>        (unsigned int) (((double) records_read)/diff_time));<br><br>}<br>int main()<br>{<br>    start_threads();<br>    return 0;<br>
}<br><br><br>Regards<br>Preeti U Murthy<br>