-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathsoftware_chronicle_enterprise_internals_impl_NativeAffinity.cpp
More file actions
116 lines (102 loc) · 2.91 KB
/
Copy pathsoftware_chronicle_enterprise_internals_impl_NativeAffinity.cpp
File metadata and controls
116 lines (102 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* Copyright 2013-2026 chronicle.software; SPDX-License-Identifier: Apache-2.0
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <jni.h>
#ifdef __linux__
#include <sched.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#endif
#include <stdexcept>
#include "software_chronicle_enterprise_internals_impl_NativeAffinity.h"
/*
* Class: software_chronicle_enterprise_internals_impl_NativeAffinity
* Method: getAffinity0
* Signature: ()J
*/
JNIEXPORT jbyteArray JNICALL Java_software_chronicle_enterprise_internals_impl_NativeAffinity_getAffinity0
(JNIEnv *env, jclass c)
{
#ifdef __linux__
// The default size of the structure supports 1024 CPUs, should be enough
// for now In the future we can use dynamic sets, which can support more
// CPUs, given OS can handle them as well
cpu_set_t mask;
const size_t size = sizeof(mask);
int res = sched_getaffinity(0, size, &mask);
if (res < 0)
{
return NULL;
}
jbyteArray ret = env->NewByteArray(size);
jbyte* bytes = env->GetByteArrayElements(ret, 0);
memcpy(bytes, &mask, size);
env->SetByteArrayRegion(ret, 0, size, bytes);
return ret;
#else
throw std::runtime_error("Not supported");
#endif
}
/*
* Class: software_chronicle_enterprise_internals_NativeAffinity
* Method: setAffinity0
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_software_chronicle_enterprise_internals_impl_NativeAffinity_setAffinity0
(JNIEnv *env, jclass c, jbyteArray affinity)
{
#ifdef __linux__
cpu_set_t mask;
const size_t size = sizeof(mask);
CPU_ZERO(&mask);
jbyte* bytes = env->GetByteArrayElements(affinity, 0);
memcpy(&mask, bytes, size);
sched_setaffinity(0, size, &mask);
#else
throw std::runtime_error("Not supported");
#endif
}
/*
* Class: software_chronicle_enterprise_internals_impl_NativeAffinity
* Method: getProcessId0
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_software_chronicle_enterprise_internals_impl_NativeAffinity_getProcessId0
(JNIEnv *env, jclass c) {
#ifndef __linux__
throw std::runtime_error("Not supported");
#else
return (jint) getpid();
#endif
}
/*
* Class: software_chronicle_enterprise_internals_impl_NativeAffinity
* Method: getThreadId0
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_software_chronicle_enterprise_internals_impl_NativeAffinity_getThreadId0
(JNIEnv *env, jclass c) {
#ifndef __linux__
throw std::runtime_error("Not supported");
#else
return (jint) (pid_t) syscall (SYS_gettid);
#endif
}
/*
* Class: software_chronicle_enterprise_internals_impl_NativeAffinity
* Method: getCpu0
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_software_chronicle_enterprise_internals_impl_NativeAffinity_getCpu0
(JNIEnv *env, jclass c) {
#ifndef __linux__
throw std::runtime_error("Not supported");
#else
return (jint) sched_getcpu();
#endif
}