Chris Wilson | b98bade | 2013-08-20 21:39:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2013 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | */ |
| 24 | |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <unistd.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <time.h> |
| 31 | #include <errno.h> |
| 32 | |
| 33 | #include "power.h" |
Chris Wilson | 9574cb1 | 2013-08-23 15:51:21 +0100 | [diff] [blame^] | 34 | #include "debugfs.h" |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 35 | |
| 36 | /* XXX Is this exposed through RAPL? */ |
| 37 | |
| 38 | int power_init(struct power *power) |
| 39 | { |
| 40 | char buf[4096]; |
| 41 | int fd, len; |
| 42 | |
| 43 | memset(power, 0, sizeof(*power)); |
| 44 | |
Chris Wilson | 9574cb1 | 2013-08-23 15:51:21 +0100 | [diff] [blame^] | 45 | sprintf(buf, "%s/i915_energy_uJ", debugfs_path); |
| 46 | fd = open(buf, 0); |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 47 | if (fd < 0) |
| 48 | return power->error = errno; |
| 49 | |
| 50 | len = read(fd, buf, sizeof(buf)); |
| 51 | close(fd); |
| 52 | |
| 53 | if (len < 0) |
| 54 | return power->error = errno; |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
Chris Wilson | 9574cb1 | 2013-08-23 15:51:21 +0100 | [diff] [blame^] | 59 | static uint64_t file_to_u64(const char *name) |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 60 | { |
| 61 | char buf[4096]; |
| 62 | int fd, len; |
| 63 | |
Chris Wilson | 9574cb1 | 2013-08-23 15:51:21 +0100 | [diff] [blame^] | 64 | sprintf(buf, "%s/i915_energy_uJ", name); |
| 65 | fd = open(buf, 0); |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 66 | if (fd < 0) |
| 67 | return 0; |
| 68 | |
| 69 | len = read(fd, buf, sizeof(buf)-1); |
| 70 | close(fd); |
| 71 | |
| 72 | if (len < 0) |
| 73 | return 0; |
| 74 | |
| 75 | buf[len] = '\0'; |
| 76 | |
| 77 | return strtoull(buf, 0, 0); |
| 78 | } |
| 79 | |
| 80 | static uint64_t clock_ms_to_u64(void) |
| 81 | { |
| 82 | struct timespec tv; |
| 83 | |
| 84 | if (clock_gettime(CLOCK_MONOTONIC, &tv) < 0) |
| 85 | return 0; |
| 86 | |
Chris Wilson | cf62d52 | 2013-08-20 10:58:02 +0100 | [diff] [blame] | 87 | return (uint64_t)tv.tv_sec * 1000 + tv.tv_nsec / 1000000; |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | int power_update(struct power *power) |
| 91 | { |
| 92 | struct power_stat *s = &power->stat[power->count++&1]; |
| 93 | struct power_stat *d = &power->stat[power->count&1]; |
| 94 | uint64_t d_time; |
| 95 | |
| 96 | if (power->error) |
| 97 | return power->error; |
| 98 | |
Chris Wilson | 9574cb1 | 2013-08-23 15:51:21 +0100 | [diff] [blame^] | 99 | s->energy = file_to_u64("i915_energy_uJ"); |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 100 | s->timestamp = clock_ms_to_u64(); |
| 101 | if (power->count == 1) |
| 102 | return EAGAIN; |
| 103 | |
| 104 | d_time = s->timestamp - d->timestamp; |
Chris Wilson | c6482b7 | 2013-08-20 11:09:11 +0100 | [diff] [blame] | 105 | if (d_time < 900) { /* HW sample rate seems to be stable ~1Hz */ |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 106 | power->count--; |
| 107 | return power->count <= 1 ? EAGAIN : 0; |
| 108 | } |
| 109 | |
| 110 | power->power_mW = (s->energy - d->energy) / d_time; |
| 111 | power->new_sample = 1; |
| 112 | return 0; |
| 113 | } |