latency.c

来自「xenomai 很好的linux实时补丁」· C语言 代码 · 共 592 行 · 第 1/2 页

C
592
字号
            {            total_hits += hits;            avg += n * hits;            if (do_histogram)                fprintf(stderr,                        "HSD|    %s| %3d -%3d | %8ld\n",                        kind,                        n,                        n+1,                        hits);            }        }    avg /= total_hits;  /* compute avg, reuse variable */    return avg;}void dump_stats (long *histogram, char* kind, double avg){    int n, total_hits = 0;    double variance = 0;    for (n = 0; n < histogram_size; n++)        {        long hits = histogram[n];        if (hits)            {            total_hits += hits;            variance += hits * (n-avg) * (n-avg);            }        }    /* compute std-deviation (unbiased form) */    variance /= total_hits - 1;    variance = sqrt(variance);    fprintf(stderr,"HSS|    %s| %9d| %10.3f| %10.3f\n",            kind, total_hits, avg, variance);}void dump_hist_stats (void){    double minavg, maxavg, avgavg;    /* max is last, where its visible w/o scrolling */    minavg = dump_histogram (histogram_min, "min");    avgavg = dump_histogram (histogram_avg, "avg");    maxavg = dump_histogram (histogram_max, "max");    fprintf(stderr,"HSH|--param|--samples-|--average--|---stddev--\n");    dump_stats (histogram_min, "min", minavg);    dump_stats (histogram_avg, "avg", avgavg);    dump_stats (histogram_max, "max", maxavg);}void cleanup_upon_sig(int sig __attribute__((unused))){    time_t actual_duration;    long gmaxj, gminj, gavgj;    if (finished)        return;    finished = 1;    if (test_mode == USER_TASK) {        rt_sem_delete(&display_sem);        gavgjitter /= (test_loops > 1 ? test_loops : 2)-1;        gminj = rt_timer_tsc2ns(gminjitter);        gmaxj = rt_timer_tsc2ns(gmaxjitter);        gavgj = rt_timer_tsc2ns(gavgjitter);    } else {        struct rtbnch_overall_result overall;        overall.histogram_min = histogram_min;        overall.histogram_max = histogram_max;        overall.histogram_avg = histogram_avg;        rt_dev_ioctl(benchdev, RTBNCH_RTIOC_STOP_TMTEST, &overall);        gminj    = overall.result.min;        gmaxj    = overall.result.max;        gavgj    = overall.result.avg;        goverrun = overall.result.overruns;    }    if (benchdev >= 0)        rt_dev_close(benchdev);    if (do_histogram || do_stats)        dump_hist_stats();    time(&test_end);    actual_duration = test_end - test_start - WARMUP_TIME;    if (!test_duration) test_duration = actual_duration;    printf("---|------------|------------|------------|--------|-------------------------\n"           "RTS|%12.3f|%12.3f|%12.3f|%8ld|    %.2ld:%.2ld:%.2ld/%.2d:%.2d:%.2d\n",           (double)gminj / 1000,           (double)gavgj / 1000,           (double)gmaxj / 1000,           goverrun,           actual_duration / 3600,           (actual_duration / 60) % 60,           actual_duration % 60,           test_duration / 3600,           (test_duration / 60) % 60,           test_duration % 60);    if (histogram_avg)  free(histogram_avg);    if (histogram_max)  free(histogram_max);    if (histogram_min)  free(histogram_min);    exit(0);}int main (int argc, char **argv){    int c, err;    while ((c = getopt(argc,argv,"hp:l:T:qH:B:sD:t:f")) != EOF)        switch (c)            {            case 'h':                do_histogram = 1;                break;            case 's':                do_stats = 1;                break;            case 'H':                histogram_size = atoi(optarg);                break;            case 'B':                bucketsize = atoi(optarg);                break;            case 'p':                period_ns = atoi(optarg) * 1000LL;                break;            case 'l':                data_lines = atoi(optarg);                break;            case 'T':                test_duration = atoi(optarg);                alarm(test_duration + WARMUP_TIME);                break;            case 'q':                quiet = 1;                break;            case 'D':                benchdev_no = atoi(optarg);                break;            case 't':                test_mode = atoi(optarg);                break;            case 'f':                freeze_max = 1;                break;            default:                fprintf(stderr, "usage: latency [options]\n"                        "  [-h]                         # print histograms of min, avg, max latencies\n"                        "  [-s]                         # print statistics of min, avg, max latencies\n"                        "  [-H <histogram-size>]        # default = 200, increase if your last bucket is full\n"                        "  [-B <bucket-size>]           # default = 1000ns, decrease for more resolution\n"                        "  [-p <period_us>]             # sampling period\n"                        "  [-l <data-lines per header>] # default=21, 0 to supress headers\n"                        "  [-T <test_duration_seconds>] # default=0, so ^C to end\n"                        "  [-q]                         # supresses RTD, RTH lines if -T is used\n"                        "  [-D <benchmark_device_no>]   # number of benchmark device, default=0\n"                        "  [-t <test_mode>]             # 0=user task (default), 1=kernel task, 2=timer IRQ\n"                        "  [-f]                         # freeze trace for each new max latency\n");                exit(2);            }    if (!test_duration && quiet)        {        fprintf(stderr, "latency: -q only works if -T has been given.\n");        quiet = 0;        }    if ((test_mode < USER_TASK) || (test_mode > TIMER_HANDLER))        {        fprintf(stderr, "latency: invalid test mode.\n");        exit(2);        }    time(&test_start);    histogram_avg = calloc(histogram_size, sizeof(long));    histogram_max = calloc(histogram_size, sizeof(long));    histogram_min = calloc(histogram_size, sizeof(long));    if (!(histogram_avg && histogram_max && histogram_min))         cleanup_upon_sig(0);    if (period_ns == 0)        period_ns = 100000LL; /* ns */    signal(SIGINT, cleanup_upon_sig);    signal(SIGTERM, cleanup_upon_sig);    signal(SIGHUP, cleanup_upon_sig);    signal(SIGALRM, cleanup_upon_sig);    setlinebuf(stdout);    printf("== Sampling period: %Ld us\n"           "== Test mode: %s\n"           "== All results in microseconds\n",           period_ns / 1000,           test_mode_names[test_mode]);    mlockall(MCL_CURRENT|MCL_FUTURE);    if ((test_mode != USER_TASK) || freeze_max)        {        char devname[RTDM_MAX_DEVNAME_LEN];        snprintf(devname, RTDM_MAX_DEVNAME_LEN, "rtbenchmark%d", benchdev_no);        benchdev = rt_dev_open(devname, O_RDWR);        if (benchdev < 0)            {            fprintf(stderr,"latency: failed to open benchmark device, code %d\n"                    "(modprobe xeno_timerbench?)\n",benchdev);            return 0;            }        }    rt_timer_set_mode(TM_ONESHOT); /* Force aperiodic timing. */    err = rt_task_create(&display_task,"display",0,98,T_FPU);    if (err)        {        fprintf(stderr,"latency: failed to create display task, code %d\n",err);        return 0;        }    err = rt_task_start(&display_task,&display,NULL);    if (err)        {        fprintf(stderr,"latency: failed to start display task, code %d\n",err);        return 0;        }    if (test_mode == USER_TASK) {        err = rt_task_create(&latency_task,"sampling",0,99,T_FPU);        if (err)            {            fprintf(stderr,"latency: failed to create latency task, code %d\n",err);            return 0;            }        err = rt_task_start(&latency_task,&latency,NULL);        if (err)            {            fprintf(stderr,"latency: failed to start latency task, code %d\n",err);            return 0;            }    }    pause();    return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?