forked from OpenSmalltalk/opensmalltalk-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CogEventLogger.class.st
521 lines (377 loc) · 11.5 KB
/
CogEventLogger.class.st
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
Class {
#name : #CogEventLogger,
#superclass : #CogClass,
#instVars : [
'cogEventBuffer',
'cogEventBufferSize',
'cogEventBufferPosition',
'cogit',
'cogEventFile',
'cogStatCounters',
'currentCounterIndex',
'coInterpreter'
],
#classVars : [
'EMPTY_COUNTER_MARK',
'INVALID_COUNTER_INDEX',
'STAT_COUNTERS_SIZE'
],
#pools : [
'CogAbstractRegisters',
'CogMethodConstants',
'LibFFIConstants'
],
#category : #'VMMaker-JIT'
}
{ #category : #translation }
CogEventLogger class >> declareCVarsIn: aCCodeGenerator [
"Declare any additional variables and/or add type declarations for existing variables."
aCCodeGenerator removeVariable: #cogit.
aCCodeGenerator removeVariable: #coInterpreter.
aCCodeGenerator
var: #cogEventBuffer
declareC: 'char cogEventBuffer[255] '.
aCCodeGenerator
var: #cogEventFile
declareC: 'FILE* cogEventFile = NULL'.
aCCodeGenerator
var: #currentCounterIndex
declareC: 'sqInt currentCounterIndex = -1'.
aCCodeGenerator
var: #cogStatCounters
declareC: 'sqInt* cogStatCounters = NULL'
]
{ #category : #initialization }
CogEventLogger class >> initializeWithOptions: options [
STAT_COUNTERS_SIZE := 4096.
EMPTY_COUNTER_MARK := BytesPerWord = 8 ifTrue: INT64_MIN ifFalse: INT32_MIN.
INVALID_COUNTER_INDEX := STAT_COUNTERS_SIZE * 2.
]
{ #category : #translation }
CogEventLogger class >> isNonArgumentImplicitReceiverVariableName: aString [
^#('cogit') includes: aString
]
{ #category : #translation }
CogEventLogger class >> mustBeGlobal: var [
^#('cogEventFile') includes: var
]
{ #category : #translation }
CogEventLogger class >> typeForSelf [
^#implicit
]
{ #category : #builder }
CogEventLogger >> addCharacter: aCharacter [
<inline: #always>
<var: #aCharacter type: #char>
cogEventBufferPosition >= cogEventBufferSize
ifFalse: [
cogEventBuffer at: cogEventBufferPosition put: aCharacter asInteger.
cogEventBufferPosition := cogEventBufferPosition + 1 ]
]
{ #category : #builder }
CogEventLogger >> addClass: aClassOop [
<inline: #always>
<var: #aClassOop type: #'sqInt'>
<var: #aBuffer type: #'char*'>
| aBuffer |
aClassOop = cogit objectMemory nilObject
ifTrue: [^ self addString: '(nil)'].
aBuffer := self alloca: 129.
cogit coInterpreter nameOfClass: aClassOop inBuffer: aBuffer withSize: 129.
self addString: aBuffer.
]
{ #category : #builder }
CogEventLogger >> addLongLong: aLongLong [
<inline: #always>
<var: #aLongLong type: #'long long'>
| size |
cogEventBufferPosition = 0
ifFalse: [ self addCharacter: $, ].
size := self
snprintf: cogEventBuffer + cogEventBufferPosition
_: cogEventBufferSize - cogEventBufferPosition
_: '%lld'
_: aLongLong.
cogEventBufferPosition := cogEventBufferPosition + size.
]
{ #category : #builder }
CogEventLogger >> addSelector: aSelectorOop [
<inline: #always>
<var: #aSelectorOop type: #'sqInt'>
| format max quantity |
aSelectorOop = cogit objectMemory nilObject
ifTrue: [^ self addString: '(nil)'].
cogEventBufferPosition = 0
ifFalse: [ self addCharacter: $, ].
format := cogit objectMemory formatOf: aSelectorOop.
format < cogit objectMemory firstByteFormat
ifTrue: [^ self addString: '(non byte format)'].
max := 128.
quantity := max min: (cogit objectMemory lengthOf: aSelectorOop).
self addCharacter: $".
0 to: quantity - 1 do: [ :anIndex |
self addCharacter: (cogit objectMemory fetchByte: anIndex ofObject: aSelectorOop )].
self addCharacter: $".
]
{ #category : #builder }
CogEventLogger >> addString: aString [
<inline: #always>
<var: #aString type: #'char*'>
| size |
cogEventBufferPosition = 0
ifFalse: [ self addCharacter: $, ].
size := self strlen: aString.
(cogEventBufferPosition + size + 2) >= cogEventBufferSize
ifFalse: [
self addCharacter: $".
self memcpy: cogEventBuffer + cogEventBufferPosition _: aString _: size.
cogEventBufferPosition := cogEventBufferPosition + size.
self addCharacter: $"]
]
{ #category : #initialization }
CogEventLogger >> allocateStatCounters [
cogStatCounters := self cCoerce: (self malloc: STAT_COUNTERS_SIZE * (self sizeof: #'sqInt')) to: #'sqInt *'.
0 to: STAT_COUNTERS_SIZE - 1 do: [ :i | cogStatCounters at: i put: EMPTY_COUNTER_MARK ]
]
{ #category : #'memory access' }
CogEventLogger >> cCoerce: value to: cTypeString [
"Type coercion. For translation a cast will be emmitted. When running in Smalltalk
answer a suitable wrapper for correct indexing."
<doNotGenerate>
^value
ifNil: [value]
ifNotNil: [value coerceTo: cTypeString sim: cogit coInterpreter]
]
{ #category : #builder }
CogEventLogger >> clearEvent [
cogEventBuffer at: 1 put: 0.
cogEventBufferSize := 254.
cogEventBufferPosition := 0.
cogEventFile isNil
ifTrue: [
cogEventFile := self fopen: 'cogEvents.csv' _: 'w+'.
cogEventFile
ifNil: [ self perror: 'fopen cogEvents.csv' ] ].
]
{ #category : #accessing }
CogEventLogger >> cogStatCounters [
<doNotGenerate>
^ cogStatCounters
]
{ #category : #accessing }
CogEventLogger >> cogStatCountersAt: anInteger [
<doNotGenerate>
^ cogStatCounters at: anInteger
]
{ #category : #accessign }
CogEventLogger >> cogStatCountersAt: anIndex put: aValue [
<doNotGenerate>
cogStatCounters at: anIndex put: aValue
]
{ #category : #accessing }
CogEventLogger >> cogit [
<doNotGenerate>
^ cogit
]
{ #category : #accessing }
CogEventLogger >> cogit: anObject [
<doNotGenerate>
cogit := anObject.
coInterpreter := cogit coInterpreter
]
{ #category : #events }
CogEventLogger >> compactCompiledCodeFinished [
self
clearEvent;
forEvent: 'compactCompiledCode';
logEvent.
]
{ #category : #counters }
CogEventLogger >> compileCountIncrement [
| counterIndex address |
<var: #address type:#sqInt>
<var: #counterIndex type:#sqInt>
<inline: true>
counterIndex := self currentCounterIndex.
address := self counterAddressFor: counterIndex.
cogit backEnd hasRISCTempReg
ifTrue: [
cogit MoveAw: address R: RISCTempReg.
cogit AddCq: 1 R: RISCTempReg.
cogit MoveR: RISCTempReg Aw: address]
ifFalse: [
cogit PushR: TempReg.
cogit MoveAw: address R: TempReg.
cogit AddCq: 1 R: TempReg.
cogit MoveR: TempReg Aw: address.
cogit PopR: TempReg ]
]
{ #category : #util }
CogEventLogger >> counterAddressFor: anInteger [
<returnTypeC: #usqInt>
^ self
cCode: [ self addressOf: (cogStatCounters at: anInteger) ]
inSmalltalk: [
(cogit simulatedAddresses
simulatedArrayVariableGetter: #cogStatCountersAt:
setter: #cogStatCountersAt:put: in: self
elementSize: (self sizeof: #'sqInt') elementCount: STAT_COUNTERS_SIZE)
addressForIndex: anInteger ]
]
{ #category : #counters }
CogEventLogger >> currentCounterIndex [
<inline: true>
<returnTypeC: #'sqInt'>
^ currentCounterIndex = -1
ifTrue: [ currentCounterIndex := self nextCounterIndex ]
ifFalse: [ currentCounterIndex ]
]
{ #category : #events }
CogEventLogger >> ensureMethodIsCoggedEvent: aCogMethod [
<api>
<var: #aCogMethod type: #'CogMethod *'>
self
clearEvent;
forEvent: 'ensureMethodIsCogged';
addClass: (cogit coInterpreter methodClassOf: aCogMethod methodObject);
addSelector: (self methodSelector: aCogMethod);
logEvent.
]
{ #category : #simulation }
CogEventLogger >> fflush: aZnCharacterWriteStream [
<doNotGenerate>
aZnCharacterWriteStream flush
]
{ #category : #simulation }
CogEventLogger >> fopen: aFileName _: aMode [
<doNotGenerate>
| inner |
aMode = 'w+' ifFalse: [ self halt ].
inner := aFileName asFileReference writeStream.
inner truncate.
^ (ZnNewLineWriterStream on: inner)
forLf;
yourself.
]
{ #category : #builder }
CogEventLogger >> forEvent: aString [
<var: #aString type: #'char*'>
self
addString: aString;
addLongLong: coInterpreter ioUTCMicrosecondsNow
]
{ #category : #simulation }
CogEventLogger >> fprintf: aWriteStream _: aString _: aCArrayAccessor [
<doNotGenerate>
| str newString delimiterPosition |
delimiterPosition := aCArrayAccessor object asString
detectIndex: [ :e | e = Character null ]
ifNone: 0.
delimiterPosition > 0
ifTrue: [ str := (aCArrayAccessor object first: delimiterPosition - 1) asString ]
ifFalse: [ str := aCArrayAccessor object asString ].
newString := aString printf: str.
aWriteStream nextPutAll: newString.
]
{ #category : #accessing }
CogEventLogger >> initialize [
<doNotGenerate>
super initialize.
currentCounterIndex := -1.
cogEventBuffer := self alloca: 255.
]
{ #category : #events }
CogEventLogger >> invalidCounterIndex [
<inline: true>
^ INVALID_COUNTER_INDEX
]
{ #category : #builder }
CogEventLogger >> logEvent [
"Terminate the string in the buffer"
cogEventBuffer
at: cogEventBufferPosition
put: 0.
cogEventBufferPosition := cogEventBufferPosition + 1.
cogEventFile ifNotNil:[
self fprintf: cogEventFile _: '%s\n' _: cogEventBuffer.
self fflush: cogEventFile ]
]
{ #category : #events }
CogEventLogger >> methodCollected: aCogMethod [
<var: #aCogMethod type: #'CogMethod *'>
cogEventBuffer at: 0 put: 0.
self
clearEvent;
forEvent: 'methodCollected';
addClass: (cogit coInterpreter methodClassOf: aCogMethod methodObject);
addSelector: (self methodSelector: aCogMethod);
addLongLong: (self numberOfCalls: aCogMethod);
logEvent.
self releaseCounter: aCogMethod counterIndex.
aCogMethod counterIndex: INVALID_COUNTER_INDEX.
]
{ #category : #events }
CogEventLogger >> methodJitted: aCogMethod [
<var: #aCogMethod type: #'CogMethod *'>
aCogMethod counterIndex: self currentCounterIndex.
self resetCounterIndex.
self
clearEvent;
forEvent: 'methodJitted';
addClass: (cogit coInterpreter methodClassOf: aCogMethod methodObject);
addSelector: (self methodSelector: aCogMethod);
logEvent.
]
{ #category : #utils }
CogEventLogger >> methodSelector: cogMethod [
<var: #cogMethod type: 'CogMethod *'>
cogMethod selector = cogit objectMemory nilObject
ifTrue: [| s |
(cogMethod cmType = CMMethod
and: [(s := cogit coInterpreter maybeSelectorOfMethod: cogMethod methodObject) notNil])
ifTrue: [ ^ s ]
ifFalse: [^ cogit objectMemory nilObject]]
ifFalse: [^ cogMethod selector].
]
{ #category : #counters }
CogEventLogger >> nextCounterIndex [
<returnTypeC: #'sqInt'>
cogStatCounters
ifNil: [ self allocateStatCounters ].
0 to: STAT_COUNTERS_SIZE - 1
do: [ :i | (cogStatCounters at: i) = EMPTY_COUNTER_MARK
ifTrue: [ cogStatCounters at: i put: 0.
^ i ] ].
self error: 'Could not find an empty counter'
]
{ #category : #counters }
CogEventLogger >> numberOfCalls: aCogMethod [
<var: #aCogMethod type: #'CogMethod *'>
<returnTypeC: #'long long'>
^ cogStatCounters at: aCogMethod counterIndex
]
{ #category : #events }
CogEventLogger >> recordDirectActivationOfQuickPrimitive: aMethodOop [
| aMethodHeader aCogMethod |
<api>
<var: #aCogMethod type: #'CogMethod *'>
aMethodHeader := coInterpreter rawHeaderOf: aMethodOop.
(coInterpreter isCogMethodReference: aMethodHeader)
ifFalse: [ ^ self ].
aCogMethod := self cCoerceSimple: aMethodHeader to: #'CogMethod *'.
cogStatCounters at: aCogMethod counterIndex put: (cogStatCounters at: aCogMethod counterIndex) + 1.
]
{ #category : #events }
CogEventLogger >> recordMethodActivatedAfterStackCheckOffset: aCogMethod [
<api>
<var: #aCogMethod type: #'CogMethod *'>
cogStatCounters at: aCogMethod counterIndex put: (cogStatCounters at: aCogMethod counterIndex) + 1
]
{ #category : #Cogit }
CogEventLogger >> releaseCounter: anInteger [
cogStatCounters at: anInteger put: EMPTY_COUNTER_MARK
]
{ #category : #counters }
CogEventLogger >> resetCounterIndex [
currentCounterIndex := -1
]