5
5
#include < cstdio>
6
6
#include < condition_variable>
7
7
8
- #define LOG_MAX_MESSAGE_SIZE (256 )
9
-
10
8
#define LOG_COLORS // TMP
11
9
12
10
#ifdef LOG_COLORS
@@ -41,11 +39,7 @@ struct gpt_log_entry {
41
39
int verbosity;
42
40
int64_t timestamp;
43
41
44
- // static-sized message
45
- char msg_stck[LOG_MAX_MESSAGE_SIZE];
46
-
47
- // if it doesn't fit in the stack, it goes here
48
- std::vector<char > msg_heap;
42
+ std::vector<char > msg;
49
43
50
44
// signals the worker thread to stop
51
45
bool is_end;
@@ -79,11 +73,7 @@ struct gpt_log_entry {
79
73
}
80
74
}
81
75
82
- if (msg_heap.empty ()) {
83
- fprintf (file, " %s" , msg_stck);
84
- } else {
85
- fprintf (file, " %s" , msg_heap.data ());
86
- }
76
+ fprintf (file, " %s" , msg.data ());
87
77
88
78
fflush (file);
89
79
}
@@ -98,7 +88,6 @@ struct gpt_log {
98
88
entries.resize (capacity);
99
89
head = 0 ;
100
90
tail = 0 ;
101
- buffer.resize (1024 );
102
91
103
92
resume ();
104
93
}
@@ -111,9 +100,7 @@ struct gpt_log {
111
100
}
112
101
113
102
private:
114
- std::mutex mtx_inp;
115
- std::mutex mtx_wrk;
116
-
103
+ std::mutex mtx;
117
104
std::thread thrd;
118
105
std::condition_variable cv;
119
106
@@ -129,39 +116,27 @@ struct gpt_log {
129
116
size_t head;
130
117
size_t tail;
131
118
132
- // print the message here before pushing
133
- std::vector< char > buffer ;
119
+ // worker thread copies into this
120
+ gpt_log_entry cur ;
134
121
135
122
public:
136
123
void add (enum ggml_log_level level, int verbosity, const char * fmt, va_list args) {
137
- std::unique_lock <std::mutex> lock_inp (mtx_inp );
124
+ std::lock_guard <std::mutex> lock (mtx );
138
125
139
126
if (!running) {
140
127
return ;
141
128
}
142
129
143
- const size_t n = vsnprintf (buffer.data (), buffer.size (), fmt, args);
144
- if (n >= buffer.size ()) {
145
- buffer.resize (n + 1 );
146
- vsnprintf (buffer.data (), buffer.size (), fmt, args);
147
- }
148
-
149
- std::lock_guard<std::mutex> lock_wrk (mtx_wrk);
150
-
151
130
auto & entry = entries[tail];
152
131
153
- if (n < LOG_MAX_MESSAGE_SIZE) {
154
- memcpy (entry.msg_stck , buffer.data (), n);
155
- entry.msg_stck [n] = ' \0 ' ;
156
- entry.msg_heap .clear ();
157
- } else {
158
- entry.msg_heap .resize (n + 1 );
159
- memcpy (entry.msg_heap .data (), buffer.data (), n);
160
- entry.msg_heap [n] = ' \0 ' ;
132
+ {
133
+ const size_t n = vsnprintf (entry.msg .data (), entry.msg .size (), fmt, args);
134
+ if (n >= entry.msg .size ()) {
135
+ entry.msg .resize (n + 1 );
136
+ vsnprintf (entry.msg .data (), entry.msg .size (), fmt, args);
137
+ }
161
138
}
162
139
163
- lock_inp.unlock ();
164
-
165
140
entry.level = level;
166
141
entry.verbosity = verbosity;
167
142
entry.timestamp = 0 ;
@@ -173,20 +148,18 @@ struct gpt_log {
173
148
tail = (tail + 1 ) % entries.size ();
174
149
if (tail == head) {
175
150
// expand the buffer
176
- size_t new_size = entries.size () * 2 ;
177
- std::vector<gpt_log_entry> new_entries (new_size);
151
+ std::vector<gpt_log_entry> new_entries (2 *entries.size ());
178
152
179
- size_t new_head = 0 ;
180
153
size_t new_tail = 0 ;
181
154
182
- while (head != tail) {
183
- new_entries[new_tail] = entries[head];
155
+ do {
156
+ new_entries[new_tail] = std::move ( entries[head]) ;
184
157
185
- head = (head + 1 ) % entries.size ();
186
- new_tail = (new_tail + 1 ) % new_size ;
187
- }
158
+ head = (head + 1 ) % entries.size ();
159
+ new_tail = (new_tail + 1 );
160
+ } while (head != tail);
188
161
189
- head = new_head ;
162
+ head = 0 ;
190
163
tail = new_tail;
191
164
192
165
entries = std::move (new_entries);
@@ -196,7 +169,7 @@ struct gpt_log {
196
169
}
197
170
198
171
void resume () {
199
- std::lock_guard<std::mutex> lock_inp (mtx_inp );
172
+ std::lock_guard<std::mutex> lock (mtx );
200
173
201
174
if (running) {
202
175
return ;
@@ -206,35 +179,37 @@ struct gpt_log {
206
179
207
180
thrd = std::thread ([this ]() {
208
181
while (true ) {
209
- std::unique_lock<std::mutex> lock_wrk (mtx_wrk);
210
- cv.wait (lock_wrk, [this ]() { return head != tail; });
182
+ {
183
+ std::unique_lock<std::mutex> lock (mtx);
184
+ cv.wait (lock, [this ]() { return head != tail; });
211
185
212
- auto & entry = entries[head];
186
+ cur = entries[head];
213
187
214
- if (entry.is_end ) {
188
+ head = (head + 1 ) % entries.size ();
189
+ }
190
+
191
+ if (cur.is_end ) {
215
192
break ;
216
193
}
217
194
218
- entry .print (stdout);
195
+ cur .print (stdout);
219
196
220
197
if (file) {
221
- entry .print (file);
198
+ cur .print (file);
222
199
}
223
-
224
- head = (head + 1 ) % entries.size ();
225
200
}
226
201
});
227
202
}
228
203
229
204
void pause () {
230
- std::lock_guard<std::mutex> lock_inp (mtx_inp);
205
+ {
206
+ std::lock_guard<std::mutex> lock (mtx);
231
207
232
- if (!running) {
233
- return ;
234
- }
208
+ if (!running) {
209
+ return ;
210
+ }
235
211
236
- {
237
- std::lock_guard<std::mutex> lock_wrk (mtx_wrk);
212
+ running = false ;
238
213
239
214
auto & entry = entries[tail];
240
215
@@ -246,13 +221,10 @@ struct gpt_log {
246
221
}
247
222
248
223
thrd.join ();
249
-
250
- running = false ;
251
224
}
252
225
253
226
void set_file (const char * path) {
254
- std::lock_guard<std::mutex> lock_inp (mtx_inp);
255
- std::lock_guard<std::mutex> lock_wrk (mtx_wrk);
227
+ pause ();
256
228
257
229
if (file) {
258
230
fclose (file);
@@ -263,11 +235,12 @@ struct gpt_log {
263
235
} else {
264
236
file = nullptr ;
265
237
}
238
+
239
+ resume ();
266
240
}
267
241
268
242
void set_timestamps (bool timestamps) {
269
- std::lock_guard<std::mutex> lock_inp (mtx_inp);
270
- std::lock_guard<std::mutex> lock_wrk (mtx_wrk);
243
+ std::lock_guard<std::mutex> lock (mtx);
271
244
272
245
this ->timestamps = timestamps;
273
246
}
0 commit comments