1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// Minimal test program for clock
18
19#include <pthread.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <time.h>
23#include <unistd.h>
24
25// this thread soaks the CPU so that clock() function will advance
26void *cpu_hog(void *arg)
27{
28    for (;;) {
29        // the system call should not be optimized away by the compiler
30        (void) getpid();
31    }
32}
33
34int main(int argc, char **argv)
35{
36    pthread_t thread;
37    clock_t ticks10, ticks15;
38
39    // do not call clock() here so we can test initialization
40
41    // soak the CPU for 10 seconds, then read clock
42    pthread_create(&thread, NULL, cpu_hog, NULL);
43    sleep(10);
44    ticks10 = clock();
45
46    // soak the CPU for 5 more seconds, then read clock
47    sleep(5);
48    ticks15 = clock();
49
50    // print the results
51    printf("CLOCKS_PER_SEC = %ld ticks/sec\n", (clock_t) CLOCKS_PER_SEC);
52    printf("At 10 secs clock=%lu, at 15 secs clock=%lu\n", ticks10, ticks15);
53
54    // exit could wait for the other thread to complete
55    _exit(EXIT_SUCCESS);
56}
57