FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpython/Modules/_tracemalloc.c at master · python-emscripten/cpython · GitHub
python-emscripten
cpython
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpython
/
Modules
/
_tracemalloc.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
1825 lines (1441 loc) · 44.9 KB
Breadcrumbs
cpython
/
Modules
/
_tracemalloc.c
Copy path
File metadata and controls
1825 lines (1441 loc) · 44.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include
"Python.h"
#include
"pycore_gc.h"
// PyGC_Head
#include
"pycore_pymem.h"
// _Py_tracemalloc_config
#include
"pycore_traceback.h"
#include
"pycore_hashtable.h"
#include
"frameobject.h"
// PyFrame_GetBack()
#include
"clinic/_tracemalloc.c.h"
/*[clinic input]
module _tracemalloc
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=708a98302fc46e5f]*/
/* Trace memory blocks allocated by PyMem_RawMalloc() */
#define
TRACE_RAW_MALLOC
/* Forward declaration */
static
void
tracemalloc_stop
(
void
);
static
void
*
raw_malloc
(
size_t
size
);
static
void
raw_free
(
void
*
ptr
);
#ifdef
Py_DEBUG
# define
TRACE_DEBUG
#endif
#define
TO_PTR
(
key
) ((const void *)(uintptr_t)(key))
#define
FROM_PTR
(
key
) ((uintptr_t)(key))
/* Protected by the GIL */
static
struct
{
PyMemAllocatorEx
mem
;
PyMemAllocatorEx
raw
;
PyMemAllocatorEx
obj
;
}
allocators
;
#if
defined(
TRACE_RAW_MALLOC
)
/* This lock is needed because tracemalloc_free() is called without
the GIL held from PyMem_RawFree(). It cannot acquire the lock because it
would introduce a deadlock in _PyThreadState_DeleteCurrent(). */
static
PyThread_type_lock
tables_lock
;
# define
TABLES_LOCK
() PyThread_acquire_lock(tables_lock, 1)
# define
TABLES_UNLOCK
() PyThread_release_lock(tables_lock)
#else
/* variables are protected by the GIL */
# define
TABLES_LOCK
()
# define
TABLES_UNLOCK
()
#endif
#define
DEFAULT_DOMAIN
0
/* Pack the frame_t structure to reduce the memory footprint on 64-bit
architectures: 12 bytes instead of 16. */
typedef
struct
#ifdef
__GNUC__
__attribute__((
packed
))
#elif
defined(
_MSC_VER
)
#pragma
pack(push, 4)
#endif
{
/* filename cannot be NULL: "<unknown>" is used if the Python frame
filename is NULL */
PyObject
*
filename
;
unsigned
int
lineno
;
}
frame_t
;
#ifdef
_MSC_VER
#pragma
pack(pop)
#endif
typedef
struct
{
Py_uhash_t
hash
;
/* Number of frames stored */
uint16_t
nframe
;
/* Total number of frames the traceback had */
uint16_t
total_nframe
;
frame_t
frames
[
1
];
}
traceback_t
;
#define
TRACEBACK_SIZE
(
NFRAME
) \
(sizeof(traceback_t) + sizeof(frame_t) * (NFRAME - 1))
/* The maximum number of frames is either:
- The maximum number of frames we can store in `traceback_t.nframe`
- The maximum memory size_t we can allocate */
static
const
unsigned long
MAX_NFRAME
=
Py_MIN
(
UINT16_MAX
, ((
SIZE_MAX
-
sizeof
(
traceback_t
)) /
sizeof
(
frame_t
)
+
1
));
static
PyObject
*
unknown_filename
=
NULL
;
static
traceback_t
tracemalloc_empty_traceback
;
/* Trace of a memory block */
typedef
struct
{
/* Size of the memory block in bytes */
size_t
size
;
/* Traceback where the memory block was allocated */
traceback_t
*
traceback
;
}
trace_t
;
/* Size in bytes of currently traced memory.
Protected by TABLES_LOCK(). */
static
size_t
tracemalloc_traced_memory
=
0
;
/* Peak size in bytes of traced memory.
Protected by TABLES_LOCK(). */
static
size_t
tracemalloc_peak_traced_memory
=
0
;
/* Hash table used as a set to intern filenames:
PyObject* => PyObject*.
Protected by the GIL */
static
_Py_hashtable_t
*
tracemalloc_filenames
=
NULL
;
/* Buffer to store a new traceback in traceback_new().
Protected by the GIL. */
static
traceback_t
*
tracemalloc_traceback
=
NULL
;
/* Hash table used as a set to intern tracebacks:
traceback_t* => traceback_t*
Protected by the GIL */
static
_Py_hashtable_t
*
tracemalloc_tracebacks
=
NULL
;
/* pointer (void*) => trace (trace_t*).
Protected by TABLES_LOCK(). */
static
_Py_hashtable_t
*
tracemalloc_traces
=
NULL
;
/* domain (unsigned int) => traces (_Py_hashtable_t).
Protected by TABLES_LOCK(). */
static
_Py_hashtable_t
*
tracemalloc_domains
=
NULL
;
#ifdef
TRACE_DEBUG
static
void
tracemalloc_error
(
const
char
*
format
, ...)
{
va_list
ap
;
fprintf
(
stderr
,
"tracemalloc: "
);
va_start
(
ap
,
format
);
vfprintf
(
stderr
,
format
,
ap
);
va_end
(
ap
);
fprintf
(
stderr
,
"\n"
);
fflush
(
stderr
);
}
#endif
#if
defined(
TRACE_RAW_MALLOC
)
#define
REENTRANT_THREADLOCAL
static
Py_tss_t
tracemalloc_reentrant_key
=
Py_tss_NEEDS_INIT
;
/* Any non-NULL pointer can be used */
#define
REENTRANT
Py_True
static
int
get_reentrant
(
void
)
{
void
*
ptr
;
assert
(
PyThread_tss_is_created
(
&
tracemalloc_reentrant_key
));
ptr
=
PyThread_tss_get
(
&
tracemalloc_reentrant_key
);
if
(
ptr
!=
NULL
) {
assert
(
ptr
==
REENTRANT
);
return
1
;
}
else
return
0
;
}
static
void
set_reentrant
(
int
reentrant
)
{
assert
(
reentrant
==
0
||
reentrant
==
1
);
assert
(
PyThread_tss_is_created
(
&
tracemalloc_reentrant_key
));
if
(
reentrant
) {
assert
(!
get_reentrant
());
PyThread_tss_set
(
&
tracemalloc_reentrant_key
,
REENTRANT
);
}
else
{
assert
(
get_reentrant
());
PyThread_tss_set
(
&
tracemalloc_reentrant_key
,
NULL
);
}
}
#else
/* TRACE_RAW_MALLOC not defined: variable protected by the GIL */
static
int
tracemalloc_reentrant
=
0
;
static
int
get_reentrant
(
void
)
{
return
tracemalloc_reentrant
;
}
static
void
set_reentrant
(
int
reentrant
)
{
assert
(
reentrant
!=
tracemalloc_reentrant
);
tracemalloc_reentrant
=
reentrant
;
}
#endif
static
Py_uhash_t
hashtable_hash_pyobject
(
const
void
*
key
)
{
PyObject
*
obj
=
(
PyObject
*
)
key
;
return
PyObject_Hash
(
obj
);
}
static
int
hashtable_compare_unicode
(
const
void
*
key1
,
const
void
*
key2
)
{
PyObject
*
obj1
=
(
PyObject
*
)
key1
;
PyObject
*
obj2
=
(
PyObject
*
)
key2
;
if
(
obj1
!=
NULL
&&
obj2
!=
NULL
) {
return
(
PyUnicode_Compare
(
obj1
,
obj2
)
==
0
);
}
else
{
return
obj1
==
obj2
;
}
}
static
Py_uhash_t
hashtable_hash_uint
(
const
void
*
key_raw
)
{
unsigned
int
key
=
(
unsigned
int
)
FROM_PTR
(
key_raw
);
return
(
Py_uhash_t
)
key
;
}
static
_Py_hashtable_t
*
hashtable_new
(
_Py_hashtable_hash_func
hash_func
,
_Py_hashtable_compare_func
compare_func
,
_Py_hashtable_destroy_func
key_destroy_func
,
_Py_hashtable_destroy_func
value_destroy_func
)
{
_Py_hashtable_allocator_t
hashtable_alloc
=
{
malloc
,
free
};
return
_Py_hashtable_new_full
(
hash_func
,
compare_func
,
key_destroy_func
,
value_destroy_func
,
&
hashtable_alloc
);
}
static
void
*
raw_malloc
(
size_t
size
)
{
return
allocators
.
raw
.
malloc
(
allocators
.
raw
.
ctx
,
size
);
}
static
void
raw_free
(
void
*
ptr
)
{
allocators
.
raw
.
free
(
allocators
.
raw
.
ctx
,
ptr
);
}
static
Py_uhash_t
hashtable_hash_traceback
(
const
void
*
key
)
{
const
traceback_t
*
traceback
=
(
const
traceback_t
*
)
key
;
return
traceback
->
hash
;
}
static
int
hashtable_compare_traceback
(
const
void
*
key1
,
const
void
*
key2
)
{
const
traceback_t
*
traceback1
=
(
const
traceback_t
*
)
key1
;
const
traceback_t
*
traceback2
=
(
const
traceback_t
*
)
key2
;
if
(
traceback1
->
nframe
!=
traceback2
->
nframe
) {
return
0
;
}
if
(
traceback1
->
total_nframe
!=
traceback2
->
total_nframe
) {
return
0
;
}
for
(
int
i
=
0
;
i
<
traceback1
->
nframe
;
i
++
) {
const
frame_t
*
frame1
=
&
traceback1
->
frames
[
i
];
const
frame_t
*
frame2
=
&
traceback2
->
frames
[
i
];
if
(
frame1
->
lineno
!=
frame2
->
lineno
) {
return
0
;
}
if
(
frame1
->
filename
!=
frame2
->
filename
) {
assert
(
PyUnicode_Compare
(
frame1
->
filename
,
frame2
->
filename
)
!=
0
);
return
0
;
}
}
return
1
;
}
static
void
tracemalloc_get_frame
(
PyFrameObject
*
pyframe
,
frame_t
*
frame
)
{
frame
->
filename
=
unknown_filename
;
int
lineno
=
PyFrame_GetLineNumber
(
pyframe
);
if
(
lineno
<
0
) {
lineno
=
0
;
}
frame
->
lineno
=
(
unsigned
int
)
lineno
;
PyCodeObject
*
code
=
PyFrame_GetCode
(
pyframe
);
PyObject
*
filename
=
code
->
co_filename
;
Py_DECREF
(
code
);
if
(
filename
==
NULL
) {
#ifdef
TRACE_DEBUG
tracemalloc_error
(
"failed to get the filename of the code object"
);
#endif
return
;
}
assert
(
filename
!=
NULL
);
if
(
filename
==
NULL
)
return
;
if
(!
PyUnicode_Check
(
filename
)) {
#ifdef
TRACE_DEBUG
tracemalloc_error
(
"filename is not a unicode string"
);
#endif
return
;
}
if
(!
PyUnicode_IS_READY
(
filename
)) {
/* Don't make a Unicode string ready to avoid reentrant calls
to tracemalloc_malloc() or tracemalloc_realloc() */
#ifdef
TRACE_DEBUG
tracemalloc_error
(
"filename is not a ready unicode string"
);
#endif
return
;
}
/* intern the filename */
_Py_hashtable_entry_t
*
entry
;
entry
=
_Py_hashtable_get_entry
(
tracemalloc_filenames
,
filename
);
if
(
entry
!=
NULL
) {
filename
=
(
PyObject
*
)
entry
->
key
;
}
else
{
/* tracemalloc_filenames is responsible to keep a reference
to the filename */
Py_INCREF
(
filename
);
if
(
_Py_hashtable_set
(
tracemalloc_filenames
,
filename
,
NULL
)
<
0
) {
Py_DECREF
(
filename
);
#ifdef
TRACE_DEBUG
tracemalloc_error
(
"failed to intern the filename"
);
#endif
return
;
}
}
/* the tracemalloc_filenames table keeps a reference to the filename */
frame
->
filename
=
filename
;
}
static
Py_uhash_t
traceback_hash
(
traceback_t
*
traceback
)
{
/* code based on tuplehash() of Objects/tupleobject.c */
Py_uhash_t
x
,
y
;
/* Unsigned for defined overflow behavior. */
int
len
=
traceback
->
nframe
;
Py_uhash_t
mult
=
_PyHASH_MULTIPLIER
;
frame_t
*
frame
;
x
=
0x345678UL
;
frame
=
traceback
->
frames
;
while
(
--
len
>=
0
) {
y
=
(
Py_uhash_t
)
PyObject_Hash
(
frame
->
filename
);
y
^= (
Py_uhash_t
)
frame
->
lineno
;
frame
++
;
x
=
(
x
^
y
)
*
mult
;
/* the cast might truncate len; that doesn't change hash stability */
mult
+=
(
Py_uhash_t
)(
82520UL
+
len
+
len
);
}
x
^=
traceback
->
total_nframe
;
x
+=
97531UL
;
return
x
;
}
static
void
traceback_get_frames
(
traceback_t
*
traceback
)
{
PyThreadState
*
tstate
=
PyGILState_GetThisThreadState
();
if
(
tstate
==
NULL
) {
#ifdef
TRACE_DEBUG
tracemalloc_error
(
"failed to get the current thread state"
);
#endif
return
;
}
PyFrameObject
*
pyframe
=
PyThreadState_GetFrame
(
tstate
);
for
(;
pyframe
!=
NULL
;) {
if
(
traceback
->
nframe
<
_Py_tracemalloc_config
.
max_nframe
) {
tracemalloc_get_frame
(
pyframe
,
&
traceback
->
frames
[
traceback
->
nframe
]);
assert
(
traceback
->
frames
[
traceback
->
nframe
].
filename
!=
NULL
);
traceback
->
nframe
++
;
}
if
(
traceback
->
total_nframe
<
UINT16_MAX
) {
traceback
->
total_nframe
++
;
}
PyFrameObject
*
back
=
PyFrame_GetBack
(
pyframe
);
Py_DECREF
(
pyframe
);
pyframe
=
back
;
}
}
static
traceback_t
*
traceback_new
(
void
)
{
traceback_t
*
traceback
;
_Py_hashtable_entry_t
*
entry
;
assert
(
PyGILState_Check
());
/* get frames */
traceback
=
tracemalloc_traceback
;
traceback
->
nframe
=
0
;
traceback
->
total_nframe
=
0
;
traceback_get_frames
(
traceback
);
if
(
traceback
->
nframe
==
0
)
return
&
tracemalloc_empty_traceback
;
traceback
->
hash
=
traceback_hash
(
traceback
);
/* intern the traceback */
entry
=
_Py_hashtable_get_entry
(
tracemalloc_tracebacks
,
traceback
);
if
(
entry
!=
NULL
) {
traceback
=
(
traceback_t
*
)
entry
->
key
;
}
else
{
traceback_t
*
copy
;
size_t
traceback_size
;
traceback_size
=
TRACEBACK_SIZE
(
traceback
->
nframe
);
copy
=
raw_malloc
(
traceback_size
);
if
(
copy
==
NULL
) {
#ifdef
TRACE_DEBUG
tracemalloc_error
(
"failed to intern the traceback: malloc failed"
);
#endif
return
NULL
;
}
memcpy
(
copy
,
traceback
,
traceback_size
);
if
(
_Py_hashtable_set
(
tracemalloc_tracebacks
,
copy
,
NULL
)
<
0
) {
raw_free
(
copy
);
#ifdef
TRACE_DEBUG
tracemalloc_error
(
"failed to intern the traceback: putdata failed"
);
#endif
return
NULL
;
}
traceback
=
copy
;
}
return
traceback
;
}
static
_Py_hashtable_t
*
tracemalloc_create_traces_table
(
void
)
{
return
hashtable_new
(
_Py_hashtable_hash_ptr
,
_Py_hashtable_compare_direct
,
NULL
,
raw_free
);
}
static
_Py_hashtable_t
*
tracemalloc_create_domains_table
(
void
)
{
return
hashtable_new
(
hashtable_hash_uint
,
_Py_hashtable_compare_direct
,
NULL
,
(
_Py_hashtable_destroy_func
)
_Py_hashtable_destroy
);
}
static
_Py_hashtable_t
*
tracemalloc_get_traces_table
(
unsigned
int
domain
)
{
if
(
domain
==
DEFAULT_DOMAIN
) {
return
tracemalloc_traces
;
}
else
{
return
_Py_hashtable_get
(
tracemalloc_domains
,
TO_PTR
(
domain
));
}
}
static
void
tracemalloc_remove_trace
(
unsigned
int
domain
,
uintptr_t
ptr
)
{
assert
(
_Py_tracemalloc_config
.
tracing
);
_Py_hashtable_t
*
traces
=
tracemalloc_get_traces_table
(
domain
);
if
(!
traces
) {
return
;
}
trace_t
*
trace
=
_Py_hashtable_steal
(
traces
,
TO_PTR
(
ptr
));
if
(!
trace
) {
return
;
}
assert
(
tracemalloc_traced_memory
>=
trace
->
size
);
tracemalloc_traced_memory
-=
trace
->
size
;
raw_free
(
trace
);
}
#define
REMOVE_TRACE
(
ptr
) \
tracemalloc_remove_trace(DEFAULT_DOMAIN, (uintptr_t)(ptr))
static
int
tracemalloc_add_trace
(
unsigned
int
domain
,
uintptr_t
ptr
,
size_t
size
)
{
assert
(
_Py_tracemalloc_config
.
tracing
);
traceback_t
*
traceback
=
traceback_new
();
if
(
traceback
==
NULL
) {
return
-1
;
}
_Py_hashtable_t
*
traces
=
tracemalloc_get_traces_table
(
domain
);
if
(
traces
==
NULL
) {
traces
=
tracemalloc_create_traces_table
();
if
(
traces
==
NULL
) {
return
-1
;
}
if
(
_Py_hashtable_set
(
tracemalloc_domains
,
TO_PTR
(
domain
),
traces
)
<
0
) {
_Py_hashtable_destroy
(
traces
);
return
-1
;
}
}
trace_t
*
trace
=
_Py_hashtable_get
(
traces
,
TO_PTR
(
ptr
));
if
(
trace
!=
NULL
) {
/* the memory block is already tracked */
assert
(
tracemalloc_traced_memory
>=
trace
->
size
);
tracemalloc_traced_memory
-=
trace
->
size
;
trace
->
size
=
size
;
trace
->
traceback
=
traceback
;
}
else
{
trace
=
raw_malloc
(
sizeof
(
trace_t
));
if
(
trace
==
NULL
) {
return
-1
;
}
trace
->
size
=
size
;
trace
->
traceback
=
traceback
;
int
res
=
_Py_hashtable_set
(
traces
,
TO_PTR
(
ptr
),
trace
);
if
(
res
!=
0
) {
raw_free
(
trace
);
return
res
;
}
}
assert
(
tracemalloc_traced_memory
<=
SIZE_MAX
-
size
);
tracemalloc_traced_memory
+=
size
;
if
(
tracemalloc_traced_memory
>
tracemalloc_peak_traced_memory
) {
tracemalloc_peak_traced_memory
=
tracemalloc_traced_memory
;
}
return
0
;
}
#define
ADD_TRACE
(
ptr
,
size
) \
tracemalloc_add_trace(DEFAULT_DOMAIN, (uintptr_t)(ptr), size)
static
void
*
tracemalloc_alloc
(
int
use_calloc
,
void
*
ctx
,
size_t
nelem
,
size_t
elsize
)
{
PyMemAllocatorEx
*
alloc
=
(
PyMemAllocatorEx
*
)
ctx
;
void
*
ptr
;
assert
(
elsize
==
0
||
nelem
<=
SIZE_MAX
/
elsize
);
if
(
use_calloc
)
ptr
=
alloc
->
calloc
(
alloc
->
ctx
,
nelem
,
elsize
);
else
ptr
=
alloc
->
malloc
(
alloc
->
ctx
,
nelem
*
elsize
);
if
(
ptr
==
NULL
)
return
NULL
;
TABLES_LOCK
();
if
(
ADD_TRACE
(
ptr
,
nelem
*
elsize
)
<
0
) {
/* Failed to allocate a trace for the new memory block */
TABLES_UNLOCK
();
alloc
->
free
(
alloc
->
ctx
,
ptr
);
return
NULL
;
}
TABLES_UNLOCK
();
return
ptr
;
}
static
void
*
tracemalloc_realloc
(
void
*
ctx
,
void
*
ptr
,
size_t
new_size
)
{
PyMemAllocatorEx
*
alloc
=
(
PyMemAllocatorEx
*
)
ctx
;
void
*
ptr2
;
ptr2
=
alloc
->
realloc
(
alloc
->
ctx
,
ptr
,
new_size
);
if
(
ptr2
==
NULL
)
return
NULL
;
if
(
ptr
!=
NULL
) {
/* an existing memory block has been resized */
TABLES_LOCK
();
/* tracemalloc_add_trace() updates the trace if there is already
a trace at address ptr2 */
if
(
ptr2
!=
ptr
) {
REMOVE_TRACE
(
ptr
);
}
if
(
ADD_TRACE
(
ptr2
,
new_size
)
<
0
) {
/* Memory allocation failed. The error cannot be reported to
the caller, because realloc() may already have shrunk the
memory block and so removed bytes.
This case is very unlikely: a hash entry has just been
released, so the hash table should have at least one free entry.
The GIL and the table lock ensures that only one thread is
allocating memory. */
Py_FatalError
(
"tracemalloc_realloc() failed to allocate a trace"
);
}
TABLES_UNLOCK
();
}
else
{
/* new allocation */
TABLES_LOCK
();
if
(
ADD_TRACE
(
ptr2
,
new_size
)
<
0
) {
/* Failed to allocate a trace for the new memory block */
TABLES_UNLOCK
();
alloc
->
free
(
alloc
->
ctx
,
ptr2
);
return
NULL
;
}
TABLES_UNLOCK
();
}
return
ptr2
;
}
static
void
tracemalloc_free
(
void
*
ctx
,
void
*
ptr
)
{
PyMemAllocatorEx
*
alloc
=
(
PyMemAllocatorEx
*
)
ctx
;
if
(
ptr
==
NULL
)
return
;
/* GIL cannot be locked in PyMem_RawFree() because it would introduce
a deadlock in _PyThreadState_DeleteCurrent(). */
alloc
->
free
(
alloc
->
ctx
,
ptr
);
TABLES_LOCK
();
REMOVE_TRACE
(
ptr
);
TABLES_UNLOCK
();
}
static
void
*
tracemalloc_alloc_gil
(
int
use_calloc
,
void
*
ctx
,
size_t
nelem
,
size_t
elsize
)
{
void
*
ptr
;
if
(
get_reentrant
()) {
PyMemAllocatorEx
*
alloc
=
(
PyMemAllocatorEx
*
)
ctx
;
if
(
use_calloc
)
return
alloc
->
calloc
(
alloc
->
ctx
,
nelem
,
elsize
);
else
return
alloc
->
malloc
(
alloc
->
ctx
,
nelem
*
elsize
);
}
/* Ignore reentrant call. PyObjet_Malloc() calls PyMem_Malloc() for
allocations larger than 512 bytes, don't trace the same memory
allocation twice. */
set_reentrant
(
1
);
ptr
=
tracemalloc_alloc
(
use_calloc
,
ctx
,
nelem
,
elsize
);
set_reentrant
(
0
);
return
ptr
;
}
static
void
*
tracemalloc_malloc_gil
(
void
*
ctx
,
size_t
size
)
{
return
tracemalloc_alloc_gil
(
0
,
ctx
,
1
,
size
);
}
static
void
*
tracemalloc_calloc_gil
(
void
*
ctx
,
size_t
nelem
,
size_t
elsize
)
{
return
tracemalloc_alloc_gil
(
1
,
ctx
,
nelem
,
elsize
);
}
static
void
*
tracemalloc_realloc_gil
(
void
*
ctx
,
void
*
ptr
,
size_t
new_size
)
{
void
*
ptr2
;
if
(
get_reentrant
()) {
/* Reentrant call to PyMem_Realloc() and PyMem_RawRealloc().
Example: PyMem_RawRealloc() is called internally by pymalloc
(_PyObject_Malloc() and _PyObject_Realloc()) to allocate a new
arena (new_arena()). */
PyMemAllocatorEx
*
alloc
=
(
PyMemAllocatorEx
*
)
ctx
;
ptr2
=
alloc
->
realloc
(
alloc
->
ctx
,
ptr
,
new_size
);
if
(
ptr2
!=
NULL
&&
ptr
!=
NULL
) {
TABLES_LOCK
();
REMOVE_TRACE
(
ptr
);
TABLES_UNLOCK
();
}
return
ptr2
;
}
/* Ignore reentrant call. PyObjet_Realloc() calls PyMem_Realloc() for
allocations larger than 512 bytes. Don't trace the same memory
allocation twice. */
set_reentrant
(
1
);
ptr2
=
tracemalloc_realloc
(
ctx
,
ptr
,
new_size
);
set_reentrant
(
0
);
return
ptr2
;
}
#ifdef
TRACE_RAW_MALLOC
static
void
*
tracemalloc_raw_alloc
(
int
use_calloc
,
void
*
ctx
,
size_t
nelem
,
size_t
elsize
)
{
PyGILState_STATE
gil_state
;
void
*
ptr
;
if
(
get_reentrant
()) {
PyMemAllocatorEx
*
alloc
=
(
PyMemAllocatorEx
*
)
ctx
;
if
(
use_calloc
)
return
alloc
->
calloc
(
alloc
->
ctx
,
nelem
,
elsize
);
else
return
alloc
->
malloc
(
alloc
->
ctx
,
nelem
*
elsize
);
}
/* Ignore reentrant call. PyGILState_Ensure() may call PyMem_RawMalloc()
indirectly which would call PyGILState_Ensure() if reentrant are not
disabled. */
set_reentrant
(
1
);
gil_state
=
PyGILState_Ensure
();
ptr
=
tracemalloc_alloc
(
use_calloc
,
ctx
,
nelem
,
elsize
);
PyGILState_Release
(
gil_state
);
set_reentrant
(
0
);
return
ptr
;
}
static
void
*
tracemalloc_raw_malloc
(
void
*
ctx
,
size_t
size
)
{
return
tracemalloc_raw_alloc
(
0
,
ctx
,
1
,
size
);
}
static
void
*
tracemalloc_raw_calloc
(
void
*
ctx
,
size_t
nelem
,
size_t
elsize
)
{
return
tracemalloc_raw_alloc
(
1
,
ctx
,
nelem
,
elsize
);
}
static
void
*
tracemalloc_raw_realloc
(
void
*
ctx
,
void
*
ptr
,
size_t
new_size
)
{
PyGILState_STATE
gil_state
;
void
*
ptr2
;
if
(
get_reentrant
()) {
/* Reentrant call to PyMem_RawRealloc(). */
PyMemAllocatorEx
*
alloc
=
(
PyMemAllocatorEx
*
)
ctx
;
ptr2
=
alloc
->
realloc
(
alloc
->
ctx
,
ptr
,
new_size
);
if
(
ptr2
!=
NULL
&&
ptr
!=
NULL
) {
TABLES_LOCK
();
REMOVE_TRACE
(
ptr
);
TABLES_UNLOCK
();
}
return
ptr2
;
}
/* Ignore reentrant call. PyGILState_Ensure() may call PyMem_RawMalloc()
indirectly which would call PyGILState_Ensure() if reentrant calls are
not disabled. */
set_reentrant
(
1
);
gil_state
=
PyGILState_Ensure
();
ptr2
=
tracemalloc_realloc
(
ctx
,
ptr
,
new_size
);
PyGILState_Release
(
gil_state
);
set_reentrant
(
0
);
return
ptr2
;
}
#endif
/* TRACE_RAW_MALLOC */
static
void
tracemalloc_clear_filename
(
void
*
value
)
{
PyObject
*
filename
=
(
PyObject
*
)
value
;
Py_DECREF
(
filename
);
}
/* reentrant flag must be set to call this function and GIL must be held */
static
void
tracemalloc_clear_traces
(
void
)
{
/* The GIL protects variables againt concurrent access */
assert
(
PyGILState_Check
());
TABLES_LOCK
();
_Py_hashtable_clear
(
tracemalloc_traces
);
_Py_hashtable_clear
(
tracemalloc_domains
);
tracemalloc_traced_memory
=
0
;
tracemalloc_peak_traced_memory
=
0
;
TABLES_UNLOCK
();
_Py_hashtable_clear
(
tracemalloc_tracebacks
);
_Py_hashtable_clear
(
tracemalloc_filenames
);
}
static
int
tracemalloc_init
(
void
)
{
if
(
_Py_tracemalloc_config
.
initialized
==
TRACEMALLOC_FINALIZED
) {
PyErr_SetString
(
PyExc_RuntimeError
,
"the tracemalloc module has been unloaded"
);
return
-1
;
}
if
(
_Py_tracemalloc_config
.
initialized
==
TRACEMALLOC_INITIALIZED
)
return
0
;
PyMem_GetAllocator
(
PYMEM_DOMAIN_RAW
,
&
allocators
.
raw
);
#ifdef
REENTRANT_THREADLOCAL
if
(
PyThread_tss_create
(
&
tracemalloc_reentrant_key
)
!=
0
) {
#ifdef
MS_WINDOWS
PyErr_SetFromWindowsErr
(
0
);
#else
PyErr_SetFromErrno
(
PyExc_OSError
);
#endif
return
-1
;
}
#endif
#if
defined(
TRACE_RAW_MALLOC
)
if
(
tables_lock
==
NULL
) {
tables_lock
=
PyThread_allocate_lock
();
if
(
tables_lock
==
NULL
) {
PyErr_SetString
(
PyExc_RuntimeError
,
"cannot allocate lock"
);
return
-1
;
}
}
#endif
tracemalloc_filenames
=
hashtable_new
(
hashtable_hash_pyobject
,
hashtable_compare_unicode
,
tracemalloc_clear_filename
,
NULL
);
tracemalloc_tracebacks
=
hashtable_new
(
hashtable_hash_traceback
,
hashtable_compare_traceback
,
NULL
,
raw_free
);
tracemalloc_traces
=
tracemalloc_create_traces_table
();
tracemalloc_domains
=
tracemalloc_create_domains_table
();
if
(
tracemalloc_filenames
==
NULL
||
tracemalloc_tracebacks
==
NULL
||
tracemalloc_traces
==
NULL
||
tracemalloc_domains
==
NULL
) {
PyErr_NoMemory
();
return
-1
;
}
unknown_filename
=
PyUnicode_FromString
(
"<unknown>"
);
if
(
unknown_filename
==
NULL
)
return
-1
;
PyUnicode_InternInPlace
(
&
unknown_filename
);
tracemalloc_empty_traceback
.
nframe
=
1
;
tracemalloc_empty_traceback
.
total_nframe
=
1
;
/* borrowed reference */
tracemalloc_empty_traceback
.
frames
[
0
].
filename
=
unknown_filename
;
tracemalloc_empty_traceback
.
frames
[
0
].
lineno
=
0
;
tracemalloc_empty_traceback
.
hash
=
traceback_hash
(
&
tracemalloc_empty_traceback
);
_Py_tracemalloc_config
.
initialized
=
TRACEMALLOC_INITIALIZED
;
return
0
;
}
static
void
tracemalloc_deinit
(
void
)
{
if
(
_Py_tracemalloc_config
.
initialized
!=
TRACEMALLOC_INITIALIZED
)
return
;
_Py_tracemalloc_config
.
initialized
=
TRACEMALLOC_FINALIZED
;
tracemalloc_stop
();
/* destroy hash tables */
_Py_hashtable_destroy
(
tracemalloc_domains
);
_Py_hashtable_destroy
(
tracemalloc_traces
);
_Py_hashtable_destroy
(
tracemalloc_tracebacks
);
_Py_hashtable_destroy
(
tracemalloc_filenames
);
#if
defined(
TRACE_RAW_MALLOC
)
if
(
tables_lock
!=
NULL
) {
PyThread_free_lock
(
tables_lock
);
tables_lock
=
NULL
;
}
#endif
#ifdef
REENTRANT_THREADLOCAL
PyThread_tss_delete
(
&
tracemalloc_reentrant_key
);
#endif
Py_XDECREF
(
unknown_filename
);
}
static
int
tracemalloc_start
(
int
max_nframe
)
{
PyMemAllocatorEx
alloc
;
size_t
size
;
if
(
max_nframe
<
1
||
(
unsigned long
)
max_nframe
>
MAX_NFRAME
) {
PyErr_Format
(
PyExc_ValueError
,
"the number of frames must be in range [1; %lu]"
,
MAX_NFRAME
);
return
-1
;
}
if
(
tracemalloc_init
()
<
0
) {
return
-1
;
}
if
(
_Py_tracemalloc_config
.
tracing
) {
/* hook already installed: do nothing */
return
0
;
}
_Py_tracemalloc_config
.
max_nframe
=
max_nframe
;
/* allocate a buffer to store a new traceback */
size
=
TRACEBACK_SIZE
(
max_nframe
);
assert
(
tracemalloc_traceback
==
NULL
);
tracemalloc_traceback
=
raw_malloc
(
size
);
if
(
tracemalloc_traceback
==
NULL
) {
PyErr_NoMemory
();
return
-1
;
}
#ifdef
TRACE_RAW_MALLOC
alloc
.
malloc
=
tracemalloc_raw_malloc
;
alloc
.
calloc
=
tracemalloc_raw_calloc
;
alloc
.
realloc
=
tracemalloc_raw_realloc
;
alloc
.
free
=
tracemalloc_free
;
alloc
.
ctx
=
&
allocators
.
raw
;
PyMem_GetAllocator
(
PYMEM_DOMAIN_RAW
,
&
allocators
.
raw
);
PyMem_SetAllocator
(
PYMEM_DOMAIN_RAW
,
&
alloc
);
#endif
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL