Back to home page

OSCL-LXR

 
 

    


0001 /* Measure nanosleep timer latency
0002  *              by: john stultz (john.stultz@linaro.org)
0003  *      (C) Copyright Linaro 2013
0004  *              Licensed under the GPLv2
0005  *
0006  *  To build:
0007  *  $ gcc nsleep-lat.c -o nsleep-lat -lrt
0008  *
0009  *   This program is free software: you can redistribute it and/or modify
0010  *   it under the terms of the GNU General Public License as published by
0011  *   the Free Software Foundation, either version 2 of the License, or
0012  *   (at your option) any later version.
0013  *
0014  *   This program is distributed in the hope that it will be useful,
0015  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0016  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017  *   GNU General Public License for more details.
0018  */
0019 
0020 #include <stdio.h>
0021 #include <stdlib.h>
0022 #include <time.h>
0023 #include <sys/time.h>
0024 #include <sys/timex.h>
0025 #include <string.h>
0026 #include <signal.h>
0027 #include "../kselftest.h"
0028 
0029 #define NSEC_PER_SEC 1000000000ULL
0030 
0031 #define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */
0032 
0033 
0034 #define CLOCK_REALTIME          0
0035 #define CLOCK_MONOTONIC         1
0036 #define CLOCK_PROCESS_CPUTIME_ID    2
0037 #define CLOCK_THREAD_CPUTIME_ID     3
0038 #define CLOCK_MONOTONIC_RAW     4
0039 #define CLOCK_REALTIME_COARSE       5
0040 #define CLOCK_MONOTONIC_COARSE      6
0041 #define CLOCK_BOOTTIME          7
0042 #define CLOCK_REALTIME_ALARM        8
0043 #define CLOCK_BOOTTIME_ALARM        9
0044 #define CLOCK_HWSPECIFIC        10
0045 #define CLOCK_TAI           11
0046 #define NR_CLOCKIDS         12
0047 
0048 #define UNSUPPORTED 0xf00f
0049 
0050 char *clockstring(int clockid)
0051 {
0052     switch (clockid) {
0053     case CLOCK_REALTIME:
0054         return "CLOCK_REALTIME";
0055     case CLOCK_MONOTONIC:
0056         return "CLOCK_MONOTONIC";
0057     case CLOCK_PROCESS_CPUTIME_ID:
0058         return "CLOCK_PROCESS_CPUTIME_ID";
0059     case CLOCK_THREAD_CPUTIME_ID:
0060         return "CLOCK_THREAD_CPUTIME_ID";
0061     case CLOCK_MONOTONIC_RAW:
0062         return "CLOCK_MONOTONIC_RAW";
0063     case CLOCK_REALTIME_COARSE:
0064         return "CLOCK_REALTIME_COARSE";
0065     case CLOCK_MONOTONIC_COARSE:
0066         return "CLOCK_MONOTONIC_COARSE";
0067     case CLOCK_BOOTTIME:
0068         return "CLOCK_BOOTTIME";
0069     case CLOCK_REALTIME_ALARM:
0070         return "CLOCK_REALTIME_ALARM";
0071     case CLOCK_BOOTTIME_ALARM:
0072         return "CLOCK_BOOTTIME_ALARM";
0073     case CLOCK_TAI:
0074         return "CLOCK_TAI";
0075     };
0076     return "UNKNOWN_CLOCKID";
0077 }
0078 
0079 struct timespec timespec_add(struct timespec ts, unsigned long long ns)
0080 {
0081     ts.tv_nsec += ns;
0082     while (ts.tv_nsec >= NSEC_PER_SEC) {
0083         ts.tv_nsec -= NSEC_PER_SEC;
0084         ts.tv_sec++;
0085     }
0086     return ts;
0087 }
0088 
0089 
0090 long long timespec_sub(struct timespec a, struct timespec b)
0091 {
0092     long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
0093 
0094     ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
0095     return ret;
0096 }
0097 
0098 int nanosleep_lat_test(int clockid, long long ns)
0099 {
0100     struct timespec start, end, target;
0101     long long latency = 0;
0102     int i, count;
0103 
0104     target.tv_sec = ns/NSEC_PER_SEC;
0105     target.tv_nsec = ns%NSEC_PER_SEC;
0106 
0107     if (clock_gettime(clockid, &start))
0108         return UNSUPPORTED;
0109     if (clock_nanosleep(clockid, 0, &target, NULL))
0110         return UNSUPPORTED;
0111 
0112     count = 10;
0113 
0114     /* First check relative latency */
0115     clock_gettime(clockid, &start);
0116     for (i = 0; i < count; i++)
0117         clock_nanosleep(clockid, 0, &target, NULL);
0118     clock_gettime(clockid, &end);
0119 
0120     if (((timespec_sub(start, end)/count)-ns) > UNRESONABLE_LATENCY) {
0121         printf("Large rel latency: %lld ns :", (timespec_sub(start, end)/count)-ns);
0122         return -1;
0123     }
0124 
0125     /* Next check absolute latency */
0126     for (i = 0; i < count; i++) {
0127         clock_gettime(clockid, &start);
0128         target = timespec_add(start, ns);
0129         clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL);
0130         clock_gettime(clockid, &end);
0131         latency += timespec_sub(target, end);
0132     }
0133 
0134     if (latency/count > UNRESONABLE_LATENCY) {
0135         printf("Large abs latency: %lld ns :", latency/count);
0136         return -1;
0137     }
0138 
0139     return 0;
0140 }
0141 
0142 
0143 
0144 int main(int argc, char **argv)
0145 {
0146     long long length;
0147     int clockid, ret;
0148 
0149     for (clockid = CLOCK_REALTIME; clockid < NR_CLOCKIDS; clockid++) {
0150 
0151         /* Skip cputime clockids since nanosleep won't increment cputime */
0152         if (clockid == CLOCK_PROCESS_CPUTIME_ID ||
0153                 clockid == CLOCK_THREAD_CPUTIME_ID ||
0154                 clockid == CLOCK_HWSPECIFIC)
0155             continue;
0156 
0157         printf("nsleep latency %-26s ", clockstring(clockid));
0158         fflush(stdout);
0159 
0160         length = 10;
0161         while (length <= (NSEC_PER_SEC * 10)) {
0162             ret = nanosleep_lat_test(clockid, length);
0163             if (ret)
0164                 break;
0165             length *= 100;
0166 
0167         }
0168 
0169         if (ret == UNSUPPORTED) {
0170             printf("[UNSUPPORTED]\n");
0171             continue;
0172         }
0173         if (ret < 0) {
0174             printf("[FAILED]\n");
0175             return ksft_exit_fail();
0176         }
0177         printf("[OK]\n");
0178     }
0179     return ksft_exit_pass();
0180 }