Pierre Ossman | 236c03c | 2014-07-04 14:12:49 +0200 | [diff] [blame] | 1 | /* Copyright 2013-2014 Pierre Ossman <ossman@cendio.se> for Cendio AB |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | #include <stdint.h> |
Pierre Ossman | 8ac3111 | 2015-02-06 13:50:36 +0100 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
Pierre Ossman | 236c03c | 2014-07-04 14:12:49 +0200 | [diff] [blame] | 22 | |
| 23 | #ifdef WIN32 |
| 24 | #include <windows.h> |
| 25 | #else |
| 26 | #include <sys/resource.h> |
| 27 | #endif |
| 28 | |
Pierre Ossman | 8ac3111 | 2015-02-06 13:50:36 +0100 | [diff] [blame] | 29 | #include "util.h" |
| 30 | |
Pierre Ossman | 236c03c | 2014-07-04 14:12:49 +0200 | [diff] [blame] | 31 | #ifdef WIN32 |
Pierre Ossman | 8ac3111 | 2015-02-06 13:50:36 +0100 | [diff] [blame] | 32 | typedef FILETIME syscounter_t; |
Pierre Ossman | 236c03c | 2014-07-04 14:12:49 +0200 | [diff] [blame] | 33 | #else |
Pierre Ossman | 8ac3111 | 2015-02-06 13:50:36 +0100 | [diff] [blame] | 34 | typedef struct rusage syscounter_t; |
Pierre Ossman | 236c03c | 2014-07-04 14:12:49 +0200 | [diff] [blame] | 35 | #endif |
| 36 | |
Pierre Ossman | 8ac3111 | 2015-02-06 13:50:36 +0100 | [diff] [blame] | 37 | static syscounter_t _globalCounter[2]; |
| 38 | static cpucounter_t globalCounter = _globalCounter; |
| 39 | |
| 40 | void startCpuCounter(void) |
| 41 | { |
| 42 | startCpuCounter(globalCounter); |
| 43 | } |
| 44 | |
| 45 | void endCpuCounter(void) |
| 46 | { |
| 47 | endCpuCounter(globalCounter); |
| 48 | } |
| 49 | |
| 50 | double getCpuCounter(void) |
| 51 | { |
| 52 | return getCpuCounter(globalCounter); |
| 53 | } |
| 54 | |
| 55 | cpucounter_t newCpuCounter(void) |
| 56 | { |
| 57 | syscounter_t *c; |
| 58 | |
| 59 | c = (syscounter_t*)malloc(sizeof(syscounter_t) * 2); |
| 60 | if (c == NULL) |
| 61 | return NULL; |
| 62 | |
| 63 | memset(c, 0, sizeof(syscounter_t) * 2); |
| 64 | |
| 65 | return c; |
| 66 | } |
| 67 | |
| 68 | void freeCpuCounter(cpucounter_t c) |
| 69 | { |
| 70 | free(c); |
| 71 | } |
| 72 | |
| 73 | static void measureCpu(syscounter_t *counter) |
Pierre Ossman | 236c03c | 2014-07-04 14:12:49 +0200 | [diff] [blame] | 74 | { |
| 75 | #ifdef WIN32 |
| 76 | FILETIME dummy1, dummy2, dummy3; |
| 77 | |
| 78 | GetProcessTimes(GetCurrentProcess(), &dummy1, &dummy2, |
Pierre Ossman | 8ac3111 | 2015-02-06 13:50:36 +0100 | [diff] [blame] | 79 | &dummy3, counter); |
Pierre Ossman | 236c03c | 2014-07-04 14:12:49 +0200 | [diff] [blame] | 80 | #else |
Pierre Ossman | 8ac3111 | 2015-02-06 13:50:36 +0100 | [diff] [blame] | 81 | getrusage(RUSAGE_SELF, counter); |
Pierre Ossman | 236c03c | 2014-07-04 14:12:49 +0200 | [diff] [blame] | 82 | #endif |
| 83 | } |
| 84 | |
Pierre Ossman | 8ac3111 | 2015-02-06 13:50:36 +0100 | [diff] [blame] | 85 | void startCpuCounter(cpucounter_t c) |
Pierre Ossman | 236c03c | 2014-07-04 14:12:49 +0200 | [diff] [blame] | 86 | { |
Pierre Ossman | 8ac3111 | 2015-02-06 13:50:36 +0100 | [diff] [blame] | 87 | syscounter_t *s = (syscounter_t*)c; |
| 88 | measureCpu(&s[0]); |
Pierre Ossman | 236c03c | 2014-07-04 14:12:49 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Pierre Ossman | 8ac3111 | 2015-02-06 13:50:36 +0100 | [diff] [blame] | 91 | void endCpuCounter(cpucounter_t c) |
Pierre Ossman | 236c03c | 2014-07-04 14:12:49 +0200 | [diff] [blame] | 92 | { |
Pierre Ossman | 8ac3111 | 2015-02-06 13:50:36 +0100 | [diff] [blame] | 93 | syscounter_t *s = (syscounter_t*)c; |
| 94 | measureCpu(&s[1]); |
Pierre Ossman | 236c03c | 2014-07-04 14:12:49 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Pierre Ossman | 8ac3111 | 2015-02-06 13:50:36 +0100 | [diff] [blame] | 97 | double getCpuCounter(cpucounter_t c) |
Pierre Ossman | 236c03c | 2014-07-04 14:12:49 +0200 | [diff] [blame] | 98 | { |
Pierre Ossman | 8ac3111 | 2015-02-06 13:50:36 +0100 | [diff] [blame] | 99 | syscounter_t *s = (syscounter_t*)c; |
Pierre Ossman | 236c03c | 2014-07-04 14:12:49 +0200 | [diff] [blame] | 100 | double seconds; |
| 101 | |
| 102 | #ifdef WIN32 |
| 103 | uint64_t counters[2]; |
| 104 | |
Pierre Ossman | 8ac3111 | 2015-02-06 13:50:36 +0100 | [diff] [blame] | 105 | counters[0] = (uint64_t)s[0].dwHighDateTime << 32 | |
| 106 | s[0].dwLowDateTime; |
| 107 | counters[1] = (uint64_t)s[1].dwHighDateTime << 32 | |
| 108 | s[1].dwLowDateTime; |
Pierre Ossman | 236c03c | 2014-07-04 14:12:49 +0200 | [diff] [blame] | 109 | |
Pierre Ossman | f10d2a7 | 2015-03-03 16:43:20 +0100 | [diff] [blame^] | 110 | seconds = (double)(counters[1] - counters[0]) / 10000000.0; |
Pierre Ossman | 236c03c | 2014-07-04 14:12:49 +0200 | [diff] [blame] | 111 | #else |
Pierre Ossman | 8ac3111 | 2015-02-06 13:50:36 +0100 | [diff] [blame] | 112 | seconds = (double)(s[1].ru_utime.tv_sec - |
| 113 | s[0].ru_utime.tv_sec); |
| 114 | seconds += (double)(s[1].ru_utime.tv_usec - |
| 115 | s[0].ru_utime.tv_usec) / 1000000.0; |
Pierre Ossman | 236c03c | 2014-07-04 14:12:49 +0200 | [diff] [blame] | 116 | #endif |
| 117 | |
| 118 | return seconds; |
| 119 | } |