Blame src/components/appio/tests/appio_test_pthreads.c

Packit Service a1973e
/* 
Packit Service a1973e
 * Test case for appio
Packit Service a1973e
 * Author: Tushar Mohan
Packit Service a1973e
 *         tusharmohan@gmail.com
Packit Service a1973e
 * 
Packit Service a1973e
 * Description: This test case reads from standard linux /etc files in
Packit Service a1973e
 *              four separate threads and copies the output to /dev/null
Packit Service a1973e
 *              READ and WRITE statistics for each of the threads is
Packit Service a1973e
 *              summarized at the end.
Packit Service a1973e
 */
Packit Service a1973e
#include <pthread.h>
Packit Service a1973e
#include <stdlib.h>
Packit Service a1973e
#include <stdio.h>
Packit Service a1973e
#include <malloc.h>
Packit Service a1973e
#include <unistd.h>
Packit Service a1973e
#include <errno.h>
Packit Service a1973e
#include <sys/types.h>
Packit Service a1973e
#include <sys/stat.h>
Packit Service a1973e
#include <fcntl.h>
Packit Service a1973e
Packit Service a1973e
#include "papi.h"
Packit Service a1973e
#include "papi_test.h"
Packit Service a1973e
Packit Service a1973e
#define NUM_EVENTS 6
Packit Service a1973e
const char* names[NUM_EVENTS] = {"READ_CALLS", "READ_BYTES","READ_USEC","WRITE_CALLS","WRITE_BYTES","WRITE_USEC"};
Packit Service a1973e
Packit Service a1973e
#define NUM_INFILES 4
Packit Service a1973e
static const char* files[NUM_INFILES] = {"/etc/passwd", "/etc/group", "/etc/protocols", "/etc/nsswitch.conf"};
Packit Service a1973e
Packit Service a1973e
void *ThreadIO(void *arg) {
Packit Service a1973e
  unsigned long tid = (unsigned long)pthread_self();
Packit Service a1973e
  if (!TESTS_QUIET) printf("\nThread %#lx: will read %s and write it to /dev/null\n", tid,(const char*) arg);
Packit Service a1973e
  int Events[NUM_EVENTS]; 
Packit Service a1973e
  long long values[NUM_EVENTS];
Packit Service a1973e
  int retval;
Packit Service a1973e
  int e;
Packit Service a1973e
  for (e=0; e
Packit Service a1973e
    retval = PAPI_event_name_to_code((char*)names[e], &Events[e]);
Packit Service a1973e
    if (retval != PAPI_OK) {
Packit Service a1973e
      fprintf(stderr, "Error getting code for %s\n", names[e]);
Packit Service a1973e
      exit(2);
Packit Service a1973e
    } 
Packit Service a1973e
  }
Packit Service a1973e
Packit Service a1973e
  /* Start counting events */
Packit Service a1973e
  if (PAPI_start_counters(Events, NUM_EVENTS) != PAPI_OK) {
Packit Service a1973e
    fprintf(stderr, "Error in PAPI_start_counters\n");
Packit Service a1973e
    exit(1);
Packit Service a1973e
  }
Packit Service a1973e
 
Packit Service a1973e
//if (PAPI_read_counters(values, NUM_EVENTS) != PAPI_OK)
Packit Service a1973e
//   handle_error(1);
Packit Service a1973e
//printf("After reading the counters: %lld\n",values[0]);
Packit Service a1973e
Packit Service a1973e
  int fdin = open((const char*)arg, O_RDONLY);
Packit Service a1973e
  if (fdin < 0) perror("Could not open file for reading: \n");
Packit Service a1973e
Packit Service a1973e
  int bytes = 0;
Packit Service a1973e
  char buf[1024];
Packit Service a1973e
Packit Service a1973e
  int fdout = open("/dev/null", O_WRONLY);
Packit Service a1973e
  if (fdout < 0) perror("Could not open /dev/null for writing: \n");
Packit Service a1973e
  while ((bytes = read(fdin, buf, 1024)) > 0) {
Packit Service a1973e
    write(fdout, buf, bytes);
Packit Service a1973e
  }
Packit Service a1973e
  close(fdout);
Packit Service a1973e
Packit Service a1973e
  /* Stop counting events */
Packit Service a1973e
  if (PAPI_stop_counters(values, NUM_EVENTS) != PAPI_OK) {
Packit Service a1973e
    fprintf(stderr, "Error in PAPI_stop_counters\n");
Packit Service a1973e
  }
Packit Service a1973e
Packit Service a1973e
  if (!TESTS_QUIET) {
Packit Service a1973e
    for (e=0; e
Packit Service a1973e
      printf("Thread %#lx: %s: %lld\n", tid, names[e], values[e]);
Packit Service a1973e
  }
Packit Service a1973e
  return(NULL);
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
int main(int argc, char** argv) {
Packit Service a1973e
  pthread_t *callThd;
Packit Service a1973e
  int i, numthrds;
Packit Service a1973e
  int retval;
Packit Service a1973e
  pthread_attr_t attr;
Packit Service a1973e
Packit Service a1973e
  /* Set TESTS_QUIET variable */
Packit Service a1973e
  tests_quiet( argc, argv );
Packit Service a1973e
Packit Service a1973e
  int version = PAPI_library_init (PAPI_VER_CURRENT);
Packit Service a1973e
  if (version != PAPI_VER_CURRENT) {
Packit Service a1973e
    fprintf(stderr, "PAPI_library_init version mismatch\n");
Packit Service a1973e
    exit(1);
Packit Service a1973e
  }
Packit Service a1973e
Packit Service a1973e
Packit Service a1973e
  pthread_attr_init(&attr);
Packit Service a1973e
  if (PAPI_thread_init(pthread_self) != PAPI_OK) {
Packit Service a1973e
    fprintf(stderr, "PAPI_thread_init returned an error\n");
Packit Service a1973e
    exit(1);
Packit Service a1973e
  }
Packit Service a1973e
#ifdef PTHREAD_CREATE_UNDETACHED
Packit Service a1973e
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_UNDETACHED);
Packit Service a1973e
#endif
Packit Service a1973e
#ifdef PTHREAD_SCOPE_SYSTEM
Packit Service a1973e
  retval = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
Packit Service a1973e
  if (retval != 0) {
Packit Service a1973e
    fprintf(stderr,"This system does not support kernel scheduled pthreads.\n");
Packit Service a1973e
    exit(1);
Packit Service a1973e
  }
Packit Service a1973e
#endif
Packit Service a1973e
Packit Service a1973e
  numthrds = NUM_INFILES;
Packit Service a1973e
  if (!TESTS_QUIET) printf("%d threads\n",numthrds);
Packit Service a1973e
  callThd = (pthread_t *)malloc(numthrds*sizeof(pthread_t));
Packit Service a1973e
Packit Service a1973e
  int rc ;
Packit Service a1973e
  for (i=0;i<(numthrds-1);i++) {
Packit Service a1973e
    rc = pthread_create(callThd+i, &attr, ThreadIO, (void *) files[i]);
Packit Service a1973e
    if (rc != 0) perror("Error creating thread using pthread_create()");
Packit Service a1973e
  }
Packit Service a1973e
  ThreadIO((void *)files[numthrds-1]);
Packit Service a1973e
  pthread_attr_destroy(&attr);
Packit Service a1973e
Packit Service a1973e
  for (i=0;i<(numthrds-1);i++)
Packit Service a1973e
    pthread_join(callThd[i], NULL);
Packit Service a1973e
Packit Service a1973e
  test_pass( __FILE__ );
Packit Service a1973e
  return 0;
Packit Service a1973e
}