OpenVPN 3 Core Library
Loading...
Searching...
No Matches
cpu.cpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#include <stdio.h>
13#include <unistd.h>
14#include <jni.h>
15
16#ifdef SWIGEXPORT
17#define EXPORT SWIGEXPORT
18#else
19#define EXPORT
20#endif
21
22#ifndef OPENVPN_PACKAGE_ID
23#error OPENVPN_PACKAGE_ID must be defined
24#endif
25
26#define MAKE_SYM2(pkg_id, suffix) Java_##pkg_id##_CPUUsage_##suffix
27#define MAKE_SYM(pkg_id, suffix) MAKE_SYM2(pkg_id, suffix)
28
29#define CPU_USAGE MAKE_SYM(OPENVPN_PACKAGE_ID, cpu_1usage)
30
31extern "C"
32{
33 jdouble CPU_USAGE(JNIEnv *env, jclass);
34};
35
36EXPORT jdouble CPU_USAGE(JNIEnv *env, jclass)
37{
38 char fnbuf[64];
39 const pid_t pid = getpid();
40 double ret = 0.0;
41
42 snprintf(fnbuf, sizeof(fnbuf), "/proc/%u/stat", (unsigned int)pid);
43 FILE *fp = fopen(fnbuf, "r");
44 if (fp)
45 {
46 double user = 0.0;
47 double system = 0.0;
48 if (fscanf(fp, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lf %lf", &user, &system) == 2)
49 ret = (user + system) / sysconf(_SC_CLK_TCK);
50 fclose(fp);
51 }
52 return ret;
53}
#define EXPORT
Definition cpu.cpp:19
#define CPU_USAGE
Definition cpu.cpp:29
std::string ret