Skip to content

Instantly share code, notes, and snippets.

@Rondom
Created July 22, 2016 12:58
Show Gist options
  • Save Rondom/24e57307a688bb64c13ad8df182ff55b to your computer and use it in GitHub Desktop.
Save Rondom/24e57307a688bb64c13ad8df182ff55b to your computer and use it in GitHub Desktop.
./thread_test 200 201 25 # 200 MB memory, 201 times create 25 threads calling lkl_sys_getpid()
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <lkl.h>
#include <lkl_host.h>
#include <time.h>
void *do_stuff(void *foo) {
// struct lkl_timespec time = {0, 5000000L};
// lkl_sys_nanosleep(&time, NULL);
lkl_sys_getpid();
lkl_stop_syscall_thread();
return NULL;
}
int run_threads(int nr_threads) {
fprintf(stderr, "Running %d threads...\n", nr_threads);
pthread_t threads[nr_threads];
for (int i = 0; i <= nr_threads; i++) {
if (pthread_create(&threads[i], NULL, do_stuff, NULL)) {
fprintf(stderr, "pthread_create: :-(\n");
return 1;
}
}
for (int i = 0; i <= nr_threads; i++) {
if (pthread_join(threads[i], NULL)) {
fprintf(stderr, "pthread_join: :(\n");
return 1;
}
}
return 0;
}
int main(int argc, char **argv) {
if (argc != 4) {
printf("Usage: %s MEM_MB ITERATIONS THREADS", argv[0]);
return 3;
}
int mem = atoi(argv[1]);
int iterations = atoi(argv[2]);
int threads = atoi(argv[3]);
fprintf(stderr, "Starting kernel...\n");
lkl_start_kernel(&lkl_host_ops, mem * 1024 * 1024, "");
lkl_register_dbg_handler();
for (int j = 0; j < iterations; j++) {
printf("Iteration %d/%d\n", j + 1, iterations);
if (run_threads(threads)) {
return 1;
}
// this is to allow the host to "recover" and not deny us pthread_create
struct timespec time = {0, 50000000L};
nanosleep(&time, NULL);
}
fprintf(stderr, "Shutting down kernel...\n");
lkl_sys_halt();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment