Commit | Line | Data |
---|---|---|
b25a8868 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; only version 2 | |
7 | * of the License. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | */ | |
18 | ||
19 | #include <stdio.h> | |
20 | #include <string.h> | |
21 | #include <sys/time.h> | |
22 | #include <unistd.h> | |
23 | ||
24 | #include "cpu.h" | |
25 | ||
3c6bae61 DG |
26 | /* |
27 | * Return cpu cycle counter | |
28 | */ | |
b25a8868 DG |
29 | cycles_t get_cycles(void) |
30 | { | |
31 | return caa_get_cycles(); | |
32 | } | |
33 | ||
3c6bae61 DG |
34 | /* |
35 | * Calculate CPU frequency. | |
36 | */ | |
b25a8868 DG |
37 | uint64_t get_cpu_freq(void) |
38 | { | |
39 | struct timezone tz; | |
40 | struct timeval tvstart, tvstop; | |
41 | cycles_t c_before, c_after; | |
42 | unsigned long microseconds; | |
43 | ||
44 | memset(&tz, 0, sizeof(tz)); | |
45 | ||
46 | gettimeofday(&tvstart, &tz); | |
47 | c_before = get_cycles(); | |
48 | gettimeofday(&tvstart, &tz); | |
49 | ||
50 | sleep(1); | |
51 | ||
52 | gettimeofday(&tvstop, &tz); | |
53 | c_after = get_cycles(); | |
54 | gettimeofday(&tvstop, &tz); | |
55 | ||
56 | microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) + | |
57 | (tvstop.tv_usec - tvstart.tv_usec); | |
58 | ||
59 | return (uint64_t) ((c_after - c_before) / microseconds); | |
60 | } |