-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathutil.cpp
More file actions
142 lines (127 loc) · 3.37 KB
/
Copy pathutil.cpp
File metadata and controls
142 lines (127 loc) · 3.37 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/****************************************************************************
Program: $Id: util.cpp 32 2015-01-10 23:05:29Z rbeverly $
Date: $Date: 2015-01-10 15:05:29 -0800 (Sat, 10 Jan 2015) $
Description: yarrp misc routines
****************************************************************************/
#include "yarrp.h"
#include <cmath>
/**
* Returns the number of milliseconds difference between
* two struct timevals
*
* @param end timeval
* @param begin timeval
* @return milliseconds
*/
uint32_t
tsdiff(struct timeval *end, struct timeval *begin) {
uint32_t diff = (end->tv_sec - begin->tv_sec) * 1000;
diff += (end->tv_usec - begin->tv_usec) / 1000;
return diff;
}
uint32_t
tsdiffus(struct timeval *end, struct timeval *begin) {
uint32_t diff = (end->tv_sec - begin->tv_sec) * 1000000;
diff += (end->tv_usec - begin->tv_usec);
return diff;
}
double
now(void) {
struct timeval now;
gettimeofday(&now, NULL);
return (double)now.tv_sec + (double)now.tv_usec / 1000000.;
}
/**
* Sigmoid function
* http://en.wikipedia.org/wiki/Sigmoid_function
*/
double
sigmoid(double t) {
return 1.0 / (1.0 + exp(-t));
}
/**
* Probability of taking an action, given an input time (t)
* and range over which to decay. Example: input milliseconds
* with a decay from 1 to 0 over an hour timespan:
* decayprob(t, 3600*1000)
*/
double
decayprob(double t, uint32_t range) {
t = t / (range / 12.0);
t -= 6;
return (1.0 - sigmoid(t));
}
double
decayprob(int32_t t, uint32_t range) {
return (decayprob((double)t, range));
}
uint8_t
randuint8() {
long val = random();
uint8_t *p = (uint8_t *) & val;
return *(p + 3);
}
bool
checkRoot() {
if ((getuid() != 0) && (geteuid() != 0)) {
cerr << "** requires root." << endl;
exit(2);
}
return true;
}
double
zrand() {
static bool seeded = false;
if (not seeded) {
srand48((long)time(NULL));
seeded = true;
}
return drand48();
}
/* generate a random libcperm key */
void permseed(uint8_t *key, uint32_t seed) {
srand(seed);
for (int i=0;i<KEYLEN/sizeof(int);i++) {
int v = rand();
memcpy(&key[(i*sizeof(int))], &v, sizeof(int));
}
}
/* generate a random libcperm key */
void permseed(uint8_t *key) {
permseed(key, time(NULL));
}
/* from: http://www.masaers.com/2013/10/08/Implementing-Poisson-pmf.html */
double poisson_pmf(const double k, const double lambda) {
return exp(k * log(lambda) - lgamma(k + 1.0) - lambda);
}
/* integral log_2 */
uint32_t intlog(uint32_t in) {
uint32_t l = 0;
while (in >>= 1) { ++l; }
return l;
}
/* Ensure that only one instance of Yarrp is running */
void instanceLock(uint8_t instance) {
const char *homedir = getenv("HOME");
if (homedir) {
char dotdir[1400];
snprintf(dotdir, 1400, "%s/.yarrp", homedir);
struct stat st = {0};
if (stat(dotdir, &st) == -1) {
mkdir(dotdir, 0755);
}
char lockfile[1500];
snprintf(lockfile, 1500, "%s/lock.%d", dotdir, instance);
int fd = open(lockfile, O_CREAT | O_RDWR, 0644);
struct flock lock;
memset(&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK;
if (fcntl(fd, F_SETLK, &lock) < 0) {
cerr << "*** " << __func__ << ": Yarrp instance already running." << endl;
exit(-1);
}
} else {
cerr << "*** " << __func__ << ": getenv" << endl;
exit(-1);
}
}