diff options
author | Chris Wilson <chris@chris-wilson.co.uk> | 2019-10-13 13:53:43 +0100 |
---|---|---|
committer | Chris Wilson <chris@chris-wilson.co.uk> | 2019-10-15 14:54:09 +0100 |
commit | e293051f8f99c72cb01d21e4b73a5928ea351eb3 (patch) | |
tree | f8d65c3f5c83a890bdc33e23428702b82edb7f04 /lib/igt_rapl.h | |
parent | 1a644320e76f6d7cb13462804b5a8897c9b7d057 (diff) |
lib: Generalise rapl interface
We can use our existing rapl interface that monitors gpu power, to also
sample the other rapl domains such as package, cores and ram.
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Andi Shyti <andi.shyti@intel.com>
Reviewed-by: Andi Shyti <andi.shyti@intel.com>
Diffstat (limited to 'lib/igt_rapl.h')
-rw-r--r-- | lib/igt_rapl.h | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/lib/igt_rapl.h b/lib/igt_rapl.h new file mode 100644 index 00000000..55c46198 --- /dev/null +++ b/lib/igt_rapl.h @@ -0,0 +1,95 @@ +/* + * Copyright © 2019 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +#ifndef IGT_RAPL_H +#define IGT_RAPL_H + +#include <stdbool.h> +#include <stdint.h> + +struct rapl { + uint64_t power, type; + double scale; + int fd; +}; + +struct power_sample { + uint64_t energy; + uint64_t time; +}; + +int rapl_open(struct rapl *r, const char *domain); + +static inline int cpu_power_open(struct rapl *r) +{ + return rapl_open(r, "cpu"); +} + +static inline int gpu_power_open(struct rapl *r) +{ + return rapl_open(r, "gpu"); +} + +static inline int pkg_power_open(struct rapl *r) +{ + return rapl_open(r, "pkg"); +} + +static inline int ram_power_open(struct rapl *r) +{ + return rapl_open(r, "ram"); +} + +static inline bool rapl_read(struct rapl *r, struct power_sample *s) +{ + return read(r->fd, s, sizeof(*s)) == sizeof(*s); +} + +static inline void rapl_close(struct rapl *r) +{ + close(r->fd); +} + +static inline double power_J(const struct rapl *r, + const struct power_sample *p0, + const struct power_sample *p1) +{ + return (p1->energy - p0->energy) * r->scale; +} + +static inline double power_s(const struct rapl *r, + const struct power_sample *p0, + const struct power_sample *p1) +{ + return (p1->time - p0->time) * 1e-9; +} + +static inline double power_W(const struct rapl *r, + const struct power_sample *p0, + const struct power_sample *p1) +{ + return power_J(r, p0, p1) / power_s(r, p0, p1); +} + +#endif /* IGT_RAPL_H */ |