int __wrap_printf(const char *format, ...) { __real_printf("calling into my printf with format=%s\n", format); va_list args; va_start(args, format); int r = vprintf(format, args); va_end(args); return r; }
static int run_sched_quota(struct smokey_test *t, int argc, char *const argv[]) { pthread_t me = pthread_self(); int ret, quota = 0, policies; struct sched_param param; cpu_set_t affinity; double effective; ret = cobalt_corectl(_CC_COBALT_GET_POLICIES, &policies, sizeof(policies)); if (ret || (policies & _CC_COBALT_SCHED_QUOTA) == 0) return -ENOSYS; CPU_ZERO(&affinity); CPU_SET(0, &affinity); ret = sched_setaffinity(0, sizeof(affinity), &affinity); if (ret) error(1, errno, "sched_setaffinity"); smokey_parse_args(t, argc, argv); pthread_mutex_init(&lock, NULL); pthread_cond_init(&barrier, NULL); sem_init(&ready, 0, 0); param.sched_priority = 50; ret = pthread_setschedparam(me, SCHED_FIFO, ¶m); if (ret) { warning("pthread_setschedparam(SCHED_FIFO, 50) failed"); return -ret; } if (SMOKEY_ARG_ISSET(sched_quota, quota)) quota = SMOKEY_ARG_INT(sched_quota, quota); if (quota <= 0) quota = 10; if (SMOKEY_ARG_ISSET(sched_quota, threads)) nrthreads = SMOKEY_ARG_INT(sched_quota, threads); if (nrthreads <= 0) nrthreads = 3; if (nrthreads > MAX_THREADS) error(1, EINVAL, "max %d threads", MAX_THREADS); calibrate(); /* Warming up, ignore result. */ loops_per_sec = calibrate(); printf("calibrating: %Lu loops/sec\n", loops_per_sec); effective = run_quota(quota); __real_printf("%d thread%s: cap=%d%%, effective=%.1f%%\n", nrthreads, nrthreads > 1 ? "s": "", quota, effective); return 0; }
int main() { #ifdef WRAP __real_printf("enter main\n"); #else printf("enter main\n"); #endif const char s[] = "Hello, World\n"; printf("%s\n", s); // it optimize to `puts`, defaulut printf("%d\n", 42); char *p = malloc(256); memcpy(p, s, sizeof(s)); printf("s=%s p=%s\n", s, p); free(p); return 0; }
void *__wrap_malloc(size_t size) { __real_printf("calling into my malloc with sz=%d\n", size); void *p = (void *)__real_malloc(size); return p; }
static double run_quota(int quota) { size_t len = sched_quota_confsz(); unsigned long long count; union sched_config cf; struct timespec req; int ret, tgid, n; double percent; char label[8]; cf.quota.op = sched_quota_add; cf.quota.add.pshared = 0; ret = sched_setconfig_np(0, SCHED_QUOTA, &cf, len); if (ret) error(1, ret, "sched_setconfig_np(add-quota-group)"); tgid = cf.quota.info.tgid; cf.quota.op = sched_quota_set; cf.quota.set.quota = quota; cf.quota.set.quota_peak = quota; cf.quota.set.tgid = tgid; ret = sched_setconfig_np(0, SCHED_QUOTA, &cf, len); if (ret) error(1, ret, "sched_setconfig_np(set-quota, tgid=%d)", tgid); printf("new thread group #%d on CPU0, quota sum is %d%%\n", tgid, cf.quota.info.quota_sum); for (n = 0; n < nrthreads; n++) { sprintf(label, "t%d", n); create_quota_thread(threads[n], label, tgid, counts[n]); sem_wait(&ready); } pthread_mutex_lock(&lock); started = 1; pthread_cond_broadcast(&barrier); pthread_mutex_unlock(&lock); req.tv_sec = TEST_SECS; req.tv_nsec = 0; clock_nanosleep(CLOCK_MONOTONIC, 0, &req, NULL); for (n = 0, count = 0; n < nrthreads; n++) { count += counts[n]; pthread_kill(threads[n], SIGDEMT); } percent = ((double)count / TEST_SECS) * 100.0 / loops_per_sec; for (n = 0; n < nrthreads; n++) { __real_printf("done quota_thread[%d], count=%lu\n", n, counts[n]); pthread_cancel(threads[n]); pthread_join(threads[n], NULL); } cf.quota.op = sched_quota_remove; cf.quota.remove.tgid = tgid; ret = sched_setconfig_np(0, SCHED_QUOTA, &cf, len); if (ret) error(1, ret, "sched_setconfig_np(remove-quota-group)"); return percent; }