| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | gettime.c |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file is a test utility, not a formal test. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | #include <unistd.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/socket.h> |
| 21 | #include <sys/un.h> |
| 22 | #include <netinet/in.h> |
| 23 | #include <netdb.h> |
| 24 | #include <time.h> |
| 25 | #include "lxtcommon.h" |
| 26 | #include "unittests.h" |
| 27 | |
| 28 | #define LXT_NAME "gettime" |
| 29 | |
| 30 | struct timespec diff(struct timespec start, struct timespec end); |
| 31 | |
| 32 | int GetTimeTestEntry(int Argc, char* Argv[]) |
| 33 | { |
| 34 | LXT_ARGS Args; |
| 35 | int ClockId; |
| 36 | int i; |
| 37 | struct timespec ProcessTime1, ProcessTime2; |
| 38 | struct timespec Resolution; |
| 39 | struct timespec ThreadTime1, ThreadTime2; |
| 40 | int Result; |
| 41 | int temp; |
| 42 | |
| 43 | LxtCheckResult(LxtInitialize(Argc, Argv, &Args, LXT_NAME)); |
| 44 | |
| 45 | // |
| 46 | // Get clock resolutions. |
| 47 | // |
| 48 | |
| 49 | for (i = 0; i < 0x20; i++) |
| 50 | { |
| 51 | ClockId = (-1 & ~0x7) | i; |
| 52 | Result = LxtClockGetRes(ClockId, &Resolution); |
| 53 | LxtLogInfo("ClockId %x First 3 bits %x Result %d %d", ClockId, i, Result, errno); |
| 54 | errno = 0; |
| 55 | } |
| 56 | |
| 57 | // |
| 58 | // Test thread and process clocks. |
| 59 | // |
| 60 | |
| 61 | LxtClockGetTime(CLOCK_THREAD_CPUTIME_ID, &ThreadTime1); |
| 62 | LxtClockGetTime(CLOCK_PROCESS_CPUTIME_ID, &ProcessTime1); |
| 63 | LxtLogInfo("ThreadTime1.tv_sec %d ThreadTime1.tv_nsec %d", ThreadTime1.tv_sec, ThreadTime1.tv_nsec); |
| 64 | |
| 65 | LxtLogInfo("ProcessTime1.tv_sec %d ProcessTime1.tv_nsec %d", ProcessTime1.tv_sec, ProcessTime1.tv_nsec); |
| 66 | |
| 67 | for (i = 0; i < 1000; i++) |
| 68 | { |
| 69 | LxtClockGetTime(CLOCK_THREAD_CPUTIME_ID, &ThreadTime2); |
| 70 | LxtLogInfo("ThreadTime2.tv_sec %d ThreadTime2.tv_nsec %d", ThreadTime2.tv_sec, ThreadTime2.tv_nsec); |
| 71 | |
| 72 | LxtClockGetTime(CLOCK_PROCESS_CPUTIME_ID, &ProcessTime2); |
| 73 | LxtLogInfo("ProcessTime2.tv_sec %d ProcessTime2.tv_nsec %d", ProcessTime2.tv_sec, ProcessTime2.tv_nsec); |
| 74 | } |
| 75 | |
| 76 | LxtLogInfo( |
| 77 | "diff(ThreadTime1,ThreadTime2).tv_sec %d diff(ThreadTime1,ThreadTime2).tv_nsec %d", |
| 78 | diff(ThreadTime1, ThreadTime2).tv_sec, |
| 79 | diff(ThreadTime1, ThreadTime2).tv_nsec); |
| 80 | |
| 81 | LxtLogInfo( |
| 82 | "diff(ProcessTime1,ProcessTime2).tv_sec %d diff(ProcessTime1,ProcessTime2).tv_nsec %d", |
| 83 | diff(ProcessTime1, ProcessTime2).tv_sec, |
| 84 | diff(ProcessTime1, ProcessTime2).tv_nsec); |
| 85 | |
| 86 | ErrorExit: |
| 87 | LxtUninitialize(); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | struct timespec diff(struct timespec start, struct timespec end) |
| 92 | { |
| 93 | struct timespec temp; |
| 94 | if ((end.tv_nsec - start.tv_nsec) < 0) |
| 95 | { |
| 96 | temp.tv_sec = end.tv_sec - start.tv_sec - 1; |
| 97 | temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec; |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | temp.tv_sec = end.tv_sec - start.tv_sec; |
| 102 | temp.tv_nsec = end.tv_nsec - start.tv_nsec; |
| 103 | } |
| 104 | return temp; |
| 105 | } |