aboutsummaryrefslogtreecommitdiff
path: root/fmdsp/fmdsp_platform_unix.c
diff options
context:
space:
mode:
authorTakamichi Horikawa <takamichiho@gmail.com>2017-04-15 01:00:20 +0900
committerTakamichi Horikawa <takamichiho@gmail.com>2017-04-15 01:00:20 +0900
commit680ab52d9e151676b8f90d105b23d2d0d89b0471 (patch)
tree5a05e82900393d2e1ecdf034cf1735d4ebfaec98 /fmdsp/fmdsp_platform_unix.c
parent428126ee4c8802a4b5f9c9ee491d54013857741b (diff)
add fmdsp fft analyzer
Diffstat (limited to 'fmdsp/fmdsp_platform_unix.c')
-rw-r--r--fmdsp/fmdsp_platform_unix.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/fmdsp/fmdsp_platform_unix.c b/fmdsp/fmdsp_platform_unix.c
new file mode 100644
index 0000000..446cee7
--- /dev/null
+++ b/fmdsp/fmdsp_platform_unix.c
@@ -0,0 +1,44 @@
+#include "fmdsp_platform_info.h"
+#include <sys/times.h>
+#include <time.h>
+#include <limits.h>
+#include <stdint.h>
+
+static struct {
+ clock_t lastall;
+ clock_t lastcpu;
+ struct timespec lasttimespec;
+} g;
+
+int fmdsp_cpu_usage(void) {
+ struct tms tmsbuf;
+ clock_t all = times(&tmsbuf);
+ clock_t cpu = tmsbuf.tms_utime + tmsbuf.tms_stime;
+ clock_t percentage = 0;
+ clock_t alld = all - g.lastall;
+ clock_t cpud = cpu - g.lastcpu;
+ if (alld) percentage = cpud * 100 / alld;
+ g.lastall = all;
+ g.lastcpu = cpu;
+ if (!g.lastall) percentage = 0;
+ if (percentage > INT_MAX) percentage = INT_MAX;
+ if (percentage < 0) percentage = 0;
+ return percentage;
+}
+
+int fmdsp_fps_30(void) {
+ struct timespec time;
+ clock_gettime(CLOCK_MONOTONIC, &time);
+ uint64_t fps = 0;
+ if (g.lasttimespec.tv_sec || g.lasttimespec.tv_nsec) {
+ uint64_t diffns = time.tv_sec - g.lasttimespec.tv_sec;
+ diffns *= 1000000000ull;
+ diffns += time.tv_nsec - g.lasttimespec.tv_nsec;
+ if (diffns) {
+ fps = 30ull * 1000000000ull / diffns;
+ }
+ }
+ g.lasttimespec = time;
+ if (fps > INT_MAX) fps = INT_MAX;
+ return fps;
+}