void *thread_sample(void *arg) { char buffer[1024]; struct timespec ts; double period = 250; double newtime; size_t size; int i; int rc; test_set_priority(pthread_self(), SCHED_FIFO, 5); DPRINTF(stderr, "Thread Sampler: started \n"); DPRINTF(stdout, "# COLUMNS %d Time TL TP ", 2 + cpus); for (i = 0; i < (cpus - 1); i++) DPRINTF(stdout, "TF%d ", i); DPRINTF(stdout, "\n"); ts.tv_sec = 0; ts.tv_nsec = period * 1000 * 1000; while (!ts_stop) { newtime = seconds_read(); size = snprintf(buffer, 1023, "%f ", newtime - base_time); for (i = 0; i < cpus + 1; i++) size += snprintf(buffer + size, 1023 - size, "%u ", tp[i].progress); DPRINTF(stdout, "%s \n", buffer); rc = nanosleep(&ts, NULL); if (rc < 0) EPRINTF("UNRESOLVED: Thread %s %d: nanosleep returned " "%d %s", tp->name, tp->index, rc, strerror(rc)); } return NULL; }
void *thread_tl(void *param) { struct thread_param *tp = param; #if __linux__ int rc; unsigned long mask = 1 << tp->cpu; rc = sched_setaffinity((pid_t) 0, sizeof(mask), &mask); if (rc < 0) { EPRINTF ("UNRESOLVED: Thread %s index %d: Can't set affinity: %d %s", tp->name, tp->index, rc, strerror(rc)); exit(UNRESOLVED); } #endif test_set_priority(pthread_self(), SCHED_FIFO, tp->priority); DPRINTF(stdout, "#EVENT %f Thread TL Started\n", seconds_read() - base_time); DPRINTF(stderr, "Thread %s index %d: started\n", tp->name, tp->index); tp->progress = 0; pthread_mutex_lock(&mutex1); pthread_mutex_lock(&mutex2); while (!tp->stop) { do_work(5, &tp->progress); } pthread_mutex_unlock(&mutex1); pthread_mutex_unlock(&mutex2); DPRINTF(stdout, "#EVENT %f Thread TL Stopped\n", seconds_read() - base_time); return NULL; }
void *thread_tb(void *arg) { int rc; struct timespec ts; ts.tv_sec = 2; ts.tv_nsec = 0; test_set_priority(pthread_self(), SCHED_FIFO, 4); DPRINTF(stderr, "Thread TB: started\n"); DPRINTF(stdout, "#EVENT %f Thread TB started,trying to lock\n", seconds_read() - base_time); rc = pthread_mutex_lock(&mutex); if (rc != 0) { EPRINTF("UNRESOLVED: Thread TB: lock returned %d %s", rc, strerror(rc)); exit(UNRESOLVED); } DPRINTF(stdout, "#EVENT %f Thread TB got lock\n", seconds_read() - base_time); nanosleep(&ts, NULL); rc = pthread_mutex_unlock(&mutex); if (rc != 0) { EPRINTF("UNRESOLVED: Thread TB: unlock returned %d %s", rc, strerror(rc)); exit(UNRESOLVED); } DPRINTF(stdout, "#EVENT %f Thread TB unlocked and stopped\n", seconds_read() - base_time); return NULL; }
void *thread_tb2(void *arg) { struct timespec boost_time; double t0, t1; int rc; test_set_priority(pthread_self(), SCHED_FIFO, 6); DPRINTF(stderr, "Thread TB2: started\n"); DPRINTF(stdout, "#EVENT %f TB2 Thread Started\n", seconds_read() - base_time); boost_time.tv_sec = time(NULL) + *(time_t *) arg; boost_time.tv_nsec = 0; t0 = seconds_read(); rc = pthread_mutex_timedlock(&mutex, &boost_time); t1 = seconds_read(); DPRINTF(stdout, "#EVENT %f TB2 Thread Waited for %.2f s\n", t1 - base_time, t1 - t0); if (rc != ETIMEDOUT && rc != 0) { EPRINTF("FAIL: Thread TB2: lock returned %d %s, " "slept %f", rc, strerror(rc), t1 - t0); exit(FAIL); } return NULL; }
void *thread_tb(void *arg) { unsigned long timeoutsec; struct timespec boost_time; double t0, t1; int rc; test_set_priority(pthread_self(), SCHED_FIFO, 4); DPRINTF(stderr,"Thread TB: started\n"); timeoutsec = *(unsigned long*) arg; DPRINTF(stdout, "#EVENT %f TB Started, waiting for mutex for %lu s\n", seconds_read() - base_time, timeoutsec); boost_time.tv_sec = time(NULL) + (time_t)timeoutsec; boost_time.tv_nsec = 0; t0 = seconds_read(); rc = pthread_mutex_timedlock(&mutex, &boost_time); t1 = seconds_read(); DPRINTF(stdout, "#EVENT %f TB Thread Waited for %.2f s\n", seconds_read() - base_time, t1 - t0); if (rc != ETIMEDOUT && rc != 0){ EPRINTF("FAIL: Thread TB: lock returned %d %s, " "slept %f", rc, strerror(rc), t1 - t0); exit(FAIL); } DPRINTF(stdout, "#EVENT %f TB Stopped\n", seconds_read() - base_time); return NULL; }
void *thread_fn(void *param) { struct thread_param *tp = param; struct timespec ts; int rc; unsigned long mask = 1 << tp->cpu; #if __linux__ rc = sched_setaffinity(0, sizeof(mask), &mask); if (rc < 0) { EPRINTF("UNRESOLVED: Thread %s index %d: Can't set affinity: " "%d %s", tp->name, tp->index, rc, strerror(rc)); exit(UNRESOLVED); } #endif test_set_priority(pthread_self(),SCHED_FIFO, tp->priority); DPRINTF(stdout, "#EVENT %f Thread %s Started\n", seconds_read() - base_time, tp->name); DPRINTF(stderr,"Thread %s index %d: started\n", tp->name, tp->index); tp->progress = 0; ts.tv_sec = 0; ts.tv_nsec = tp->sleep_ms * 1000 * 1000; if (tp->index == 0) pthread_mutex_lock(&mutex1); while (!tp->stop) { do_work(5, &tp->progress); if (tp->sleep_ms == 0) continue; rc = nanosleep(&ts, NULL); if (rc < 0) { EPRINTF("UNRESOLVED: Thread %s %d: nanosleep returned " "%d %s", tp->name, tp->index, rc, strerror(rc)); exit(UNRESOLVED); } } if (tp->index == 0) pthread_mutex_unlock(&mutex1); DPRINTF(stdout, "#EVENT %f Thread %s Stopped\n", seconds_read() - base_time, tp->name); return NULL; }
void *thread_tb(void *arg) { unsigned long timeoutsec; struct timespec boost_time; double seconds, t0, t1; int rc; test_set_priority(pthread_self(), SCHED_FIFO, 4); DPRINTF(stdout, "#EVENT %f TB Starts\n", seconds_read() - base_time); timeoutsec = *(unsigned long*) arg; boost_time.tv_sec = time(NULL) + (time_t)timeoutsec; boost_time.tv_nsec = 0; t0 = seconds_read(); rc = pthread_mutex_timedlock(&mutex, &boost_time); t1 = seconds_read(); seconds = t1 - t0; DPRINTF(stdout, "#EVENT %f TB Waited for %.2f s\n", t1 - base_time, seconds); if (rc != ETIMEDOUT) { //EPRINTF("FAIL: Thread TB: lock returned %d %s, " // , rc, strerror(rc)); EPRINTF("#WARNING: The system call pthread_mutex_timedlock() FAILED! Returned %d,(%s). But actually expired!",rc, strerror(rc)); EPRINTF("#WARNING:The time set for waiting a mutex is %ld s, but the actual waiting time is %.2f \n ", (time_t)timeoutsec, seconds); //exit(FAIL); } DPRINTF(stderr, "Thread TB: DONE. lock returned %d %s, " "slept %f \n", rc, strerror(rc), seconds) return NULL; }
int main(int argc, char **argv) { pthread_mutexattr_t mutex_attr; pthread_attr_t threadattr; pthread_t threads[cpus - 1], threadsample, threadtp, threadtl, threadtb; time_t multiplier = 1; int i; int rc; test_set_priority(pthread_self(), SCHED_FIFO, 6); base_time = seconds_read(); cpus = sysconf(_SC_NPROCESSORS_ONLN); /* Initialize a mutex with PTHREAD_PRIO_INHERIT protocol */ mutex_attr_init(&mutex_attr); mutex_init(&mutex, &mutex_attr); /* Initialize thread attr */ threadattr_init(&threadattr); /* Start the sample thread */ DPRINTF(stderr, "Main Thread: Creating sample thread\n"); rc = pthread_create(&threadsample, &threadattr, thread_sample, NULL); if (rc != 0) { EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc)); exit(UNRESOLVED); } /* Start the TF threads */ DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", cpus - 1); for (i = 0; i < cpus - 1; i++) { rc = pthread_create(&threads[i], &threadattr, thread_fn, &tp[i + 2]); if (rc != 0) { EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc)); exit(UNRESOLVED); } } sleep(base_time + multiplier * 10 - seconds_read()); /* Start TP thread */ DPRINTF(stderr, "Main Thread: Creating TP thread\n"); rc = pthread_create(&threadtp, &threadattr, thread_fn, &tp[1]); if (rc != 0) { EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc)); exit(UNRESOLVED); } sleep(base_time + multiplier * 20 - seconds_read()); /* Start TL thread */ DPRINTF(stderr, "Main Thread: Creating TL thread\n"); rc = pthread_create(&threadtl, &threadattr, thread_tl, &tp[0]); if (rc != 0) { EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc)); exit(UNRESOLVED); } sleep(base_time + multiplier * 30 - seconds_read()); /* Start TB thread (boosting thread) */ rc = pthread_create(&threadtb, &threadattr, thread_tb, NULL); if (rc != 0) { EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc)); exit(UNRESOLVED); } sleep(base_time + multiplier * 40 - seconds_read()); unlock_mutex = 1; sleep(base_time + multiplier * 50 - seconds_read()); /* Stop TL thread */ tp[0].stop = 1; sleep(base_time + multiplier * 60 - seconds_read()); /* Stop TP thread */ tp[1].stop = 1; sleep(base_time + multiplier * 70 - seconds_read()); /* Stop TF threads */ for (i = 2; i < cpus - 1; i++) { tp[i].stop = 1; } /* Stop sampler */ ts_stop = 1; DPRINTF(stderr, "Main Thread: stop sampler thread\n"); return 0; }
int main(int argc, char **argv) { pthread_mutexattr_t mutex_attr; pthread_attr_t threadattr; pthread_t *threads, threadsample, threadtp, threadtl, threadtb; int multiplier = 1; int i; int rc; test_set_priority(pthread_self(), SCHED_FIFO, 6); cpus = sysconf(_SC_NPROCESSORS_ONLN); threads = malloc((cpus - 1) * sizeof(pthread_t)); if (threads == NULL) return -ENOMEM; tp = malloc((cpus + 1) * sizeof(struct thread_param)); if (tp == NULL) { free(threads); return -ENOMEM; } else { set_thread_param(0, 0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0); set_thread_param(1, 1, 0, 50, 2, SCHED_FIFO, "TP", 0, 0, 0, 0); for(i = 1; i <= cpus-1; i++) set_thread_param(i + 1, i + 1, 0, 0, 3, SCHED_FIFO, "TF", i, 0, 0, 0); } base_time = seconds_read(); /* Initialize a mutex with PTHREAD_PRIO_INHERIT protocol */ mutex_attr_init(&mutex_attr); mutex_init(&mutex, &mutex_attr); /* Initialize thread attr */ threadattr_init(&threadattr); /* Start the sample thread */ DPRINTF(stderr,"Main Thread: start sample thread \n"); rc = pthread_create(&threadsample, &threadattr, thread_sample, NULL); if (rc != 0) { EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc)); exit(UNRESOLVED); } /* Start the TF threads */ DPRINTF(stderr,"Main Thread: start %d TF thread\n", cpus-1); for (i = 0; i < cpus - 1; i++) { rc = pthread_create(&threads[i], &threadattr, thread_fn, &tp[i + 2]); if (rc != 0) { EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc)); exit(UNRESOLVED); } } sleep(base_time + multiplier * 10 - seconds_read()); /* Start TP thread */ DPRINTF(stderr,"Main Thread: start TP thread\n"); rc = pthread_create(&threadtp, &threadattr, thread_fn, &tp[1]); if (rc != 0) { EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc)); exit(UNRESOLVED); } sleep(base_time + multiplier * 20 - seconds_read()); /* Start TL thread */ DPRINTF(stderr,"Main Thread: start TL thread\n"); rc = pthread_create(&threadtl, &threadattr, thread_tl, &tp[0]); if (rc != 0) { EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc)); exit(UNRESOLVED); } sleep(base_time + multiplier * 30 - seconds_read()); /* Start TB thread (boosting thread) */ DPRINTF(stderr,"Main Thread: start TB thread\n"); int timeout = multiplier * 20; rc = pthread_create(&threadtb, &threadattr, thread_tb, &timeout); if (rc != 0) { EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc)); exit(UNRESOLVED); } sleep(base_time + multiplier * 60 - seconds_read()); /* Stop TL thread */ DPRINTF(stderr,"Main Thread: stop TL thread\n"); tp[0].stop = 1; sleep(base_time + multiplier * 70 - seconds_read()); /* Stop TP thread */ DPRINTF(stderr,"Main Thread: stop TP thread\n"); tp[1].stop = 1; sleep(base_time + multiplier * 80 - seconds_read()); /* Stop TF threads */ DPRINTF(stderr,"Main Thread: stop TF threads\n"); for (i = 2; i < cpus - 1; i++) { tp[i].stop = 1; } /* Stop sampler */ ts_stop = 1; DPRINTF(stderr,"Main Thread: stop sampler thread \n"); free(tp); free(threads); return 0; }