Russ Cox | 219fb02 | 2009-10-22 10:59:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | Plan 9 from User Space src/lib9/time.c |
| 3 | http://code.swtch.com/plan9port/src/tip/src/lib9/time.c |
| 4 | |
| 5 | Copyright 2001-2007 Russ Cox. All Rights Reserved. |
| 6 | |
| 7 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | of this software and associated documentation files (the "Software"), to deal |
| 9 | in the Software without restriction, including without limitation the rights |
| 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | copies of the Software, and to permit persons to whom the Software is |
| 12 | furnished to do so, subject to the following conditions: |
| 13 | |
| 14 | The above copyright notice and this permission notice shall be included in |
| 15 | all copies or substantial portions of the Software. |
| 16 | |
| 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | THE SOFTWARE. |
| 24 | */ |
| 25 | #include <u.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <time.h> |
Hector Chu | cd9d72b | 2009-11-30 11:53:11 -0800 | [diff] [blame] | 28 | #ifndef __MINGW32__ |
Russ Cox | 219fb02 | 2009-10-22 10:59:27 -0700 | [diff] [blame] | 29 | #include <sys/resource.h> |
Hector Chu | cd9d72b | 2009-11-30 11:53:11 -0800 | [diff] [blame] | 30 | #endif |
Russ Cox | 219fb02 | 2009-10-22 10:59:27 -0700 | [diff] [blame] | 31 | #define NOPLAN9DEFINES |
| 32 | #include <libc.h> |
| 33 | |
| 34 | long |
| 35 | p9times(long *t) |
| 36 | { |
Hector Chu | cd9d72b | 2009-11-30 11:53:11 -0800 | [diff] [blame] | 37 | #ifdef __MINGW32__ |
| 38 | memset(t, 0, 4*sizeof(long)); |
| 39 | #else |
Russ Cox | 219fb02 | 2009-10-22 10:59:27 -0700 | [diff] [blame] | 40 | struct rusage ru, cru; |
| 41 | |
| 42 | if(getrusage(0, &ru) < 0 || getrusage(-1, &cru) < 0) |
| 43 | return -1; |
| 44 | |
| 45 | t[0] = ru.ru_utime.tv_sec*1000 + ru.ru_utime.tv_usec/1000; |
| 46 | t[1] = ru.ru_stime.tv_sec*1000 + ru.ru_stime.tv_usec/1000; |
| 47 | t[2] = cru.ru_utime.tv_sec*1000 + cru.ru_utime.tv_usec/1000; |
| 48 | t[3] = cru.ru_stime.tv_sec*1000 + cru.ru_stime.tv_usec/1000; |
Hector Chu | cd9d72b | 2009-11-30 11:53:11 -0800 | [diff] [blame] | 49 | #endif |
Russ Cox | 219fb02 | 2009-10-22 10:59:27 -0700 | [diff] [blame] | 50 | |
| 51 | /* BUG */ |
| 52 | return t[0]+t[1]+t[2]+t[3]; |
| 53 | } |
| 54 | |
| 55 | double |
| 56 | p9cputime(void) |
| 57 | { |
| 58 | long t[4]; |
| 59 | double d; |
| 60 | |
| 61 | if(p9times(t) < 0) |
| 62 | return -1.0; |
| 63 | |
| 64 | d = (double)t[0]+(double)t[1]+(double)t[2]+(double)t[3]; |
| 65 | return d/1000.0; |
| 66 | } |