/
dummyload.cc
181 lines (157 loc) · 2.94 KB
/
dummyload.cc
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "muduo/base/Atomic.h"
#include "muduo/base/Condition.h"
#include "muduo/base/CurrentThread.h"
#include "muduo/base/Mutex.h"
#include "muduo/base/Thread.h"
#include "muduo/base/Timestamp.h"
#include "muduo/net/EventLoop.h"
#include <math.h>
#include <stdio.h>
using namespace muduo;
using namespace muduo::net;
int g_cycles = 0;
int g_percent = 82;
AtomicInt32 g_done;
bool g_busy = false;
MutexLock g_mutex;
Condition g_cond(g_mutex);
double busy(int cycles)
{
double result = 0;
for (int i = 0; i < cycles; ++i)
{
result += sqrt(i) * sqrt(i+1);
}
return result;
}
double getSeconds(int cycles)
{
Timestamp start = Timestamp::now();
busy(cycles);
return timeDifference(Timestamp::now(), start);
}
void findCycles()
{
g_cycles = 1000;
while (getSeconds(g_cycles) < 0.001)
g_cycles = g_cycles + g_cycles / 4; // * 1.25
printf("cycles %d\n", g_cycles);
}
void threadFunc()
{
while (g_done.get() == 0)
{
{
MutexLockGuard guard(g_mutex);
while (!g_busy)
g_cond.wait();
}
busy(g_cycles);
}
printf("thread exit\n");
}
// this is open-loop control
void load(int percent)
{
percent = std::max(0, percent);
percent = std::min(100, percent);
// Bresenham's line algorithm
int err = 2*percent - 100;
int count = 0;
for (int i = 0; i < 100; ++i)
{
bool busy = false;
if (err > 0)
{
busy = true;
err += 2*(percent - 100);
++count;
// printf("%2d, ", i);
}
else
{
err += 2*percent;
}
{
MutexLockGuard guard(g_mutex);
g_busy = busy;
g_cond.notifyAll();
}
CurrentThread::sleepUsec(10*1000); // 10 ms
}
assert(count == percent);
}
void fixed()
{
while (true)
{
load(g_percent);
}
}
void cosine()
{
while (true)
for (int i = 0; i < 200; ++i)
{
int percent = static_cast<int>((1.0 + cos(i * 3.14159 / 100)) / 2 * g_percent + 0.5);
load(percent);
}
}
void sawtooth()
{
while (true)
for (int i = 0; i <= 100; ++i)
{
int percent = static_cast<int>(i / 100.0 * g_percent);
load(percent);
}
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
printf("Usage: %s [fctsz] [percent] [num_threads]\n", argv[0]);
return 0;
}
printf("pid %d\n", getpid());
findCycles();
g_percent = argc > 2 ? atoi(argv[2]) : 43;
int numThreads = argc > 3 ? atoi(argv[3]) : 1;
std::vector<std::unique_ptr<Thread>> threads;
for (int i = 0; i < numThreads; ++i)
{
threads.emplace_back(new Thread(threadFunc));
threads.back()->start();
}
switch (argv[1][0])
{
case 'f':
{
fixed();
}
break;
case 'c':
{
cosine();
}
break;
case 'z':
{
sawtooth();
}
break;
// TODO: square and triangle waves
default:
break;
}
g_done.getAndSet(1);
{
MutexLockGuard guard(g_mutex);
g_busy = true;
g_cond.notifyAll();
}
for (int i = 0; i < numThreads; ++i)
{
threads[i]->join();
}
}