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 | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 25 | #include <sys/stat.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | #include <unistd.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <time.h> |
| 32 | #include <errno.h> |
| 33 | |
Tvrtko Ursulin | a688dec | 2017-09-13 17:38:16 +0100 | [diff] [blame] | 34 | #include "igt_perf.h" |
| 35 | |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 36 | #include "rc6.h" |
Chris Wilson | 65d2481 | 2013-08-27 15:19:58 +0100 | [diff] [blame] | 37 | |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 38 | int rc6_init(struct rc6 *rc6) |
| 39 | { |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 40 | memset(rc6, 0, sizeof(*rc6)); |
| 41 | |
Tvrtko Ursulin | 4c57ff4 | 2017-11-24 17:16:18 +0000 | [diff] [blame] | 42 | rc6->fd = perf_i915_open(I915_PMU_RC6_RESIDENCY); |
| 43 | if (rc6->fd < 0) { |
Chris Wilson | 65d2481 | 2013-08-27 15:19:58 +0100 | [diff] [blame] | 44 | struct stat st; |
| 45 | if (stat("/sys/class/drm/card0/power", &st) < 0) |
| 46 | return rc6->error = errno; |
| 47 | } |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static uint64_t file_to_u64(const char *path) |
| 53 | { |
| 54 | char buf[4096]; |
| 55 | int fd, len; |
| 56 | |
| 57 | fd = open(path, 0); |
| 58 | if (fd < 0) |
Chris Wilson | 65d2481 | 2013-08-27 15:19:58 +0100 | [diff] [blame] | 59 | return -1; |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 60 | |
| 61 | len = read(fd, buf, sizeof(buf)-1); |
| 62 | close(fd); |
| 63 | |
| 64 | if (len < 0) |
Chris Wilson | 65d2481 | 2013-08-27 15:19:58 +0100 | [diff] [blame] | 65 | return -1; |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 66 | |
| 67 | buf[len] = '\0'; |
| 68 | |
| 69 | return strtoull(buf, 0, 0); |
| 70 | } |
| 71 | |
| 72 | static uint64_t clock_ms_to_u64(void) |
| 73 | { |
| 74 | struct timespec tv; |
| 75 | |
| 76 | if (clock_gettime(CLOCK_MONOTONIC, &tv) < 0) |
| 77 | return 0; |
| 78 | |
Chris Wilson | cf62d52 | 2013-08-20 10:58:02 +0100 | [diff] [blame] | 79 | return (uint64_t)tv.tv_sec * 1000 + tv.tv_nsec / 1000000; |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | int rc6_update(struct rc6 *rc6) |
| 83 | { |
| 84 | struct rc6_stat *s = &rc6->stat[rc6->count++&1]; |
| 85 | struct rc6_stat *d = &rc6->stat[rc6->count&1]; |
| 86 | uint64_t d_time, d_rc6, d_rc6p, d_rc6pp; |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 87 | |
| 88 | if (rc6->error) |
| 89 | return rc6->error; |
| 90 | |
Tvrtko Ursulin | 4c57ff4 | 2017-11-24 17:16:18 +0000 | [diff] [blame] | 91 | if (rc6->fd < 0) { |
Chris Wilson | 65d2481 | 2013-08-27 15:19:58 +0100 | [diff] [blame] | 92 | struct stat st; |
Chris Wilson | c888507 | 2013-08-20 11:08:13 +0100 | [diff] [blame] | 93 | |
Chris Wilson | 65d2481 | 2013-08-27 15:19:58 +0100 | [diff] [blame] | 94 | if (stat("/sys/class/drm/card0/power/rc6_residency_ms", &st) < 0) |
| 95 | return rc6->error = ENOENT; |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 96 | |
Chris Wilson | 65d2481 | 2013-08-27 15:19:58 +0100 | [diff] [blame] | 97 | s->rc6_residency = file_to_u64("/sys/class/drm/card0/power/rc6_residency_ms"); |
| 98 | s->rc6p_residency = file_to_u64("/sys/class/drm/card0/power/rc6p_residency_ms"); |
| 99 | s->rc6pp_residency = file_to_u64("/sys/class/drm/card0/power/rc6pp_residency_ms"); |
| 100 | s->timestamp = clock_ms_to_u64(); |
| 101 | } else { |
Tvrtko Ursulin | 4c57ff4 | 2017-11-24 17:16:18 +0000 | [diff] [blame] | 102 | uint64_t data[2]; |
Chris Wilson | 65d2481 | 2013-08-27 15:19:58 +0100 | [diff] [blame] | 103 | |
Tvrtko Ursulin | 4c57ff4 | 2017-11-24 17:16:18 +0000 | [diff] [blame] | 104 | if (read(rc6->fd, data, sizeof(data)) < sizeof(data)) |
Chris Wilson | 65d2481 | 2013-08-27 15:19:58 +0100 | [diff] [blame] | 105 | return rc6->error = errno; |
| 106 | |
Tvrtko Ursulin | 4c57ff4 | 2017-11-24 17:16:18 +0000 | [diff] [blame] | 107 | s->timestamp = data[1] / 1e6; |
| 108 | s->rc6_residency = data[0] / 1e6; |
Chris Wilson | 65d2481 | 2013-08-27 15:19:58 +0100 | [diff] [blame] | 109 | } |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 110 | |
| 111 | if (rc6->count == 1) |
| 112 | return EAGAIN; |
| 113 | |
| 114 | d_time = s->timestamp - d->timestamp; |
Chris Wilson | 65d2481 | 2013-08-27 15:19:58 +0100 | [diff] [blame] | 115 | if (d_time == 0) { |
| 116 | rc6->count--; |
| 117 | return EAGAIN; |
| 118 | } |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 119 | |
| 120 | d_rc6 = s->rc6_residency - d->rc6_residency; |
Tvrtko Ursulin | 0d8385a | 2017-09-13 18:28:24 +0100 | [diff] [blame] | 121 | rc6->rc6 = 100 * d_rc6 / d_time; |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 122 | |
| 123 | d_rc6p = s->rc6p_residency - d->rc6p_residency; |
Tvrtko Ursulin | 0d8385a | 2017-09-13 18:28:24 +0100 | [diff] [blame] | 124 | rc6->rc6p = 100 * d_rc6p / d_time; |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 125 | |
| 126 | d_rc6pp = s->rc6pp_residency - d->rc6pp_residency; |
Tvrtko Ursulin | 0d8385a | 2017-09-13 18:28:24 +0100 | [diff] [blame] | 127 | rc6->rc6pp = 100 * d_rc6pp / d_time; |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 128 | |
Tvrtko Ursulin | 0d8385a | 2017-09-13 18:28:24 +0100 | [diff] [blame] | 129 | rc6->rc6_combined = 100 * (d_rc6 + d_rc6p + d_rc6pp) / d_time; |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 130 | return 0; |
| 131 | } |