forked from phacility/xhprof
-
Notifications
You must be signed in to change notification settings - Fork 167
/
xhprof.c
executable file
·1626 lines (1334 loc) · 41.7 KB
/
xhprof.c
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
/*
* Copyright (c) 2009 Facebook
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_xhprof.h"
#include "zend_extensions.h"
#ifndef ZEND_WIN32
# include <sys/time.h>
# include <sys/resource.h>
# include <unistd.h>
#else
# include "win32/time.h"
# include "win32/getrusage.h"
# include "win32/unistd.h"
#endif
#include <stdlib.h>
#if HAVE_PCRE
#include "ext/pcre/php_pcre.h"
#endif
#if __APPLE__
#include <mach/mach_init.h>
#include <mach/mach_time.h>
#endif
#ifdef ZEND_WIN32
LARGE_INTEGER performance_frequency;
#endif
ZEND_DECLARE_MODULE_GLOBALS(xhprof)
/**
* ****************************
* STATIC FUNCTION DECLARATIONS
* ****************************
*/
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(arginfo_xhprof_enable, 0, 0, 0)
ZEND_ARG_INFO(0, flags)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_xhprof_disable, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_xhprof_sample_enable, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_xhprof_sample_disable, 0)
ZEND_END_ARG_INFO()
/* }}} */
/**
* *********************
* PHP EXTENSION GLOBALS
* *********************
*/
/* List of functions implemented/exposed by xhprof */
zend_function_entry xhprof_functions[] = {
PHP_FE(xhprof_enable, arginfo_xhprof_enable)
PHP_FE(xhprof_disable, arginfo_xhprof_disable)
PHP_FE(xhprof_sample_enable, arginfo_xhprof_sample_enable)
PHP_FE(xhprof_sample_disable, arginfo_xhprof_sample_disable)
{NULL, NULL, NULL}
};
/* Callback functions for the xhprof extension */
zend_module_entry xhprof_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"xhprof", /* Name of the extension */
xhprof_functions, /* List of functions exposed */
PHP_MINIT(xhprof), /* Module init callback */
PHP_MSHUTDOWN(xhprof), /* Module shutdown callback */
PHP_RINIT(xhprof), /* Request init callback */
PHP_RSHUTDOWN(xhprof), /* Request shutdown callback */
PHP_MINFO(xhprof), /* Module info callback */
#if ZEND_MODULE_API_NO >= 20010901
XHPROF_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
PHP_INI_BEGIN()
/* output directory:
* Currently this is not used by the extension itself.
* But some implementations of iXHProfRuns interface might
* choose to save/restore XHProf profiler runs in the
* directory specified by this ini setting.
*/
PHP_INI_ENTRY("xhprof.output_dir", "", PHP_INI_ALL, NULL)
/*
* collect_additional_info
* Collect mysql_query, curl_exec internal info. The default is 0.
*/
STD_PHP_INI_ENTRY("xhprof.collect_additional_info", "0", PHP_INI_ALL, OnUpdateBool, collect_additional_info, zend_xhprof_globals, xhprof_globals)
/* sampling_interval:
* Sampling interval to be used by the sampling profiler, in microseconds.
*/
#define STRINGIFY_(X) #X
#define STRINGIFY(X) STRINGIFY_(X)
STD_PHP_INI_ENTRY("xhprof.sampling_interval", STRINGIFY(XHPROF_DEFAULT_SAMPLING_INTERVAL), PHP_INI_ALL, OnUpdateLong, sampling_interval, zend_xhprof_globals, xhprof_globals)
/* sampling_depth:
* Depth to trace call-chain by the sampling profiler
*/
STD_PHP_INI_ENTRY("xhprof.sampling_depth", STRINGIFY(INT_MAX), PHP_INI_ALL, OnUpdateLong, sampling_depth, zend_xhprof_globals, xhprof_globals)
PHP_INI_END()
/* Init module */
#ifdef COMPILE_DL_XHPROF
ZEND_GET_MODULE(xhprof)
#endif
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE();
#endif
/**
* **********************************
* PHP EXTENSION FUNCTION DEFINITIONS
* **********************************
*/
/**
* Start XHProf profiling in hierarchical mode.
*
* @param long $flags flags for hierarchical mode
* @return void
* @author kannan
*/
PHP_FUNCTION(xhprof_enable)
{
zend_long xhprof_flags = 0; /* XHProf flags */
zval *optional_array = NULL; /* optional array arg: for future use */
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|lz", &xhprof_flags, &optional_array) == FAILURE) {
return;
}
hp_get_ignored_functions_from_arg(optional_array);
hp_begin(XHPROF_MODE_HIERARCHICAL, xhprof_flags);
}
/**
* Stops XHProf from profiling in hierarchical mode anymore and returns the
* profile info.
*
* @param void
* @return array hash-array of XHProf's profile info
* @author kannan, hzhao
*/
PHP_FUNCTION(xhprof_disable)
{
if (XHPROF_G(enabled)) {
hp_stop();
RETURN_ZVAL(&XHPROF_G(stats_count), 1, 0);
}
/* else null is returned */
}
/**
* Start XHProf profiling in sampling mode.
*
* @return void
* @author cjiang
*/
PHP_FUNCTION(xhprof_sample_enable)
{
zend_long xhprof_flags = 0; /* XHProf flags */
hp_get_ignored_functions_from_arg(NULL);
hp_begin(XHPROF_MODE_SAMPLED, xhprof_flags);
}
/**
* Stops XHProf from profiling in sampling mode anymore and returns the profile
* info.
*
* @param void
* @return array hash-array of XHProf's profile info
* @author cjiang
*/
PHP_FUNCTION(xhprof_sample_disable)
{
if (XHPROF_G(enabled)) {
hp_stop();
RETURN_ZVAL(&XHPROF_G(stats_count), 1, 0);
}
/* else null is returned */
}
static void php_xhprof_init_globals(zend_xhprof_globals *xhprof_globals)
{
xhprof_globals->enabled = 0;
xhprof_globals->ever_enabled = 0;
xhprof_globals->xhprof_flags = 0;
xhprof_globals->entries = NULL;
xhprof_globals->root = NULL;
xhprof_globals->trace_callbacks = NULL;
xhprof_globals->ignored_functions = NULL;
xhprof_globals->sampling_interval = XHPROF_DEFAULT_SAMPLING_INTERVAL;
xhprof_globals->sampling_depth = INT_MAX;
ZVAL_UNDEF(&xhprof_globals->stats_count);
/* no free hp_entry_t structures to start with */
xhprof_globals->entry_free_list = NULL;
int i;
for (i = 0; i < XHPROF_FUNC_HASH_COUNTERS_SIZE; i++) {
xhprof_globals->func_hash_counters[i] = 0;
}
if (xhprof_globals->sampling_interval < XHPROF_MINIMAL_SAMPLING_INTERVAL) {
xhprof_globals->sampling_interval = XHPROF_MINIMAL_SAMPLING_INTERVAL;
}
}
/**
* Module init callback.
*
* @author cjiang
*/
PHP_MINIT_FUNCTION(xhprof)
{
ZEND_INIT_MODULE_GLOBALS(xhprof, php_xhprof_init_globals, NULL);
REGISTER_INI_ENTRIES();
hp_register_constants(INIT_FUNC_ARGS_PASSTHRU);
/* Replace zend_compile with our proxy */
_zend_compile_file = zend_compile_file;
zend_compile_file = hp_compile_file;
/* Replace zend_compile_string with our proxy */
_zend_compile_string = zend_compile_string;
zend_compile_string = hp_compile_string;
/* Replace zend_execute with our proxy */
_zend_execute_ex = zend_execute_ex;
zend_execute_ex = hp_execute_ex;
/* Replace zend_execute_internal with our proxy */
_zend_execute_internal = zend_execute_internal;
zend_execute_internal = hp_execute_internal;
#if defined(DEBUG)
/* To make it random number generator repeatable to ease testing. */
srand(0);
#endif
#ifdef ZEND_WIN32
QueryPerformanceFrequency(&performance_frequency);
#endif
return SUCCESS;
}
/**
* Module shutdown callback.
*/
PHP_MSHUTDOWN_FUNCTION(xhprof)
{
/* free any remaining items in the free list */
hp_free_the_free_list();
/* Remove proxies, restore the originals */
zend_execute_ex = _zend_execute_ex;
zend_execute_internal = _zend_execute_internal;
zend_compile_file = _zend_compile_file;
zend_compile_string = _zend_compile_string;
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/**
* Request init callback. Nothing to do yet!
*/
PHP_RINIT_FUNCTION(xhprof)
{
#if defined(ZTS) && defined(COMPILE_DL_XHPROF)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
XHPROF_G(timebase_conversion) = get_timebase_conversion();
return SUCCESS;
}
/**
* Request shutdown callback. Stop profiling and return.
*/
PHP_RSHUTDOWN_FUNCTION(xhprof)
{
hp_end();
return SUCCESS;
}
/**
* Module info callback. Returns the xhprof version.
*/
PHP_MINFO_FUNCTION(xhprof)
{
php_info_print_table_start();
php_info_print_table_header(2, "xhprof support", "enabled");
php_info_print_table_row(2, "Version", XHPROF_VERSION);
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/**
* ***************************************************
* COMMON HELPER FUNCTION DEFINITIONS AND LOCAL MACROS
* ***************************************************
*/
static void hp_register_constants(INIT_FUNC_ARGS)
{
REGISTER_LONG_CONSTANT("XHPROF_FLAGS_NO_BUILTINS",
XHPROF_FLAGS_NO_BUILTINS,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("XHPROF_FLAGS_CPU",
XHPROF_FLAGS_CPU,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("XHPROF_FLAGS_MEMORY",
XHPROF_FLAGS_MEMORY,
CONST_CS | CONST_PERSISTENT);
}
/**
* Parse the list of ignored functions from the zval argument.
*
* @author mpal
*/
void hp_get_ignored_functions_from_arg(zval *args)
{
if (args == NULL) {
return;
}
zval *pzval = zend_hash_str_find(Z_ARRVAL_P(args), "ignored_functions", sizeof("ignored_functions") - 1);
XHPROF_G(ignored_functions) = hp_ignored_functions_init(pzval);
}
void hp_ignored_functions_clear(hp_ignored_functions *functions)
{
if (functions == NULL) {
return;
}
hp_array_del(functions->names);
functions->names = NULL;
memset(functions->filter, 0, XHPROF_MAX_IGNORED_FUNCTIONS);
efree(functions);
}
double get_timebase_conversion()
{
#if defined(__APPLE__)
mach_timebase_info_data_t info;
(void) mach_timebase_info(&info);
return (info.numer / info.denom) * 1000;
#endif
return 1.0;
}
hp_ignored_functions *hp_ignored_functions_init(zval *values)
{
hp_ignored_functions *functions;
zend_string **names;
uint32_t ix = 0;
int count;
/* Delete the array storing ignored function names */
hp_ignored_functions_clear(XHPROF_G(ignored_functions));
if (!values) {
return NULL;
}
if (Z_TYPE_P(values) == IS_ARRAY) {
HashTable *ht;
zend_ulong num_key;
zend_string *key;
zval *val;
ht = Z_ARRVAL_P(values);
count = zend_hash_num_elements(ht);
names = ecalloc(count + 1, sizeof(zend_string *));
ZEND_HASH_FOREACH_KEY_VAL(ht, num_key, key, val) {
if (!key) {
if (Z_TYPE_P(val) == IS_STRING && strcmp(Z_STRVAL_P(val), ROOT_SYMBOL) != 0) {
/* do not ignore "main" */
names[ix] = zend_string_init(Z_STRVAL_P(val), Z_STRLEN_P(val), 0);
ix++;
}
}
} ZEND_HASH_FOREACH_END();
} else if (Z_TYPE_P(values) == IS_STRING) {
names = ecalloc(2, sizeof(zend_string *));
names[0] = zend_string_init(Z_STRVAL_P(values), Z_STRLEN_P(values), 0);
ix = 1;
} else {
return NULL;
}
/* NULL terminate the array */
names[ix] = NULL;
functions = emalloc(sizeof(hp_ignored_functions));
functions->names = names;
memset(functions->filter, 0, XHPROF_MAX_IGNORED_FUNCTIONS);
uint32_t i = 0;
for (; names[i] != NULL; i++) {
zend_ulong hash = ZSTR_HASH(names[i]);
int idx = hash % XHPROF_MAX_IGNORED_FUNCTIONS;
functions->filter[idx] = hash;
}
return functions;
}
/**
* Check if function collides in filter of functions to be ignored.
*
* @author mpal
*/
int hp_ignored_functions_filter_collision(hp_ignored_functions *functions, zend_ulong hash)
{
zend_ulong idx = hash % XHPROF_MAX_IGNORED_FUNCTIONS;
return functions->filter[idx];
}
/**
* Initialize profiler state
*
* @author kannan, veeve
*/
void hp_init_profiler_state(int level)
{
/* Setup globals */
if (!XHPROF_G(ever_enabled)) {
XHPROF_G(ever_enabled) = 1;
XHPROF_G(entries) = NULL;
}
XHPROF_G(profiler_level) = (int)level;
/* Init stats_count */
if (Z_TYPE(XHPROF_G(stats_count)) != IS_UNDEF) {
zval_ptr_dtor(&XHPROF_G(stats_count));
}
array_init(&XHPROF_G(stats_count));
hp_init_trace_callbacks();
/* Call current mode's init cb */
XHPROF_G(mode_cb).init_cb();
}
/**
* Cleanup profiler state
*
* @author kannan, veeve
*/
void hp_clean_profiler_state()
{
/* Call current mode's exit cb */
XHPROF_G(mode_cb).exit_cb();
/* Clear globals */
if (Z_TYPE(XHPROF_G(stats_count)) != IS_UNDEF) {
zval_ptr_dtor(&XHPROF_G(stats_count));
}
ZVAL_UNDEF(&XHPROF_G(stats_count));
XHPROF_G(entries) = NULL;
XHPROF_G(profiler_level) = 1;
XHPROF_G(ever_enabled) = 0;
if (XHPROF_G(trace_callbacks)) {
zend_hash_destroy(XHPROF_G(trace_callbacks));
FREE_HASHTABLE(XHPROF_G(trace_callbacks));
XHPROF_G(trace_callbacks) = NULL;
}
/* Delete the array storing ignored function names */
hp_ignored_functions_clear(XHPROF_G(ignored_functions));
XHPROF_G(ignored_functions) = NULL;
}
/**
* Returns formatted function name
*
* @param entry hp_entry
* @author veeve
*/
size_t hp_get_entry_name(hp_entry_t *entry, char *result_buf, size_t result_len)
{
size_t len;
/* Add '@recurse_level' if required */
/* NOTE: Dont use snprintf's return val as it is compiler dependent */
if (entry->rlvl_hprof) {
len = snprintf(result_buf, result_len, "%s@%d", ZSTR_VAL(entry->name_hprof), entry->rlvl_hprof);
} else {
len = snprintf(result_buf, result_len, "%s", ZSTR_VAL(entry->name_hprof));
}
return len;
}
/**
* Check if this entry should be ignored, first with a conservative Bloomish
* filter then with an exact check against the function names.
*
* @author mpal
*/
int hp_ignore_entry_work(zend_ulong hash_code, zend_string *curr_func)
{
if (XHPROF_G(ignored_functions) == NULL) {
return 0;
}
hp_ignored_functions *functions = XHPROF_G(ignored_functions);
if (hp_ignored_functions_filter_collision(functions, hash_code)) {
int i = 0;
for (; functions->names[i] != NULL; i++) {
zend_string *name = functions->names[i];
if (zend_string_equals(curr_func, name)) {
return 1;
}
}
}
return 0;
}
/**
* Build a caller qualified name for a callee.
*
* For example, if A() is caller for B(), then it returns "A==>B".
* Recursive invokations are denoted with @<n> where n is the recursion
* depth.
*
* For example, "foo==>foo@1", and "foo@2==>foo@3" are examples of direct
* recursion. And "bar==>foo@1" is an example of an indirect recursive
* call to foo (implying the foo() is on the call stack some levels
* above).
*
* @author kannan, veeve
*/
size_t hp_get_function_stack(hp_entry_t *entry, int level, char *result_buf, size_t result_len)
{
size_t len = 0;
/* End recursion if we dont need deeper levels or we dont have any deeper
* levels */
if (!entry->prev_hprof || (level <= 1)) {
return hp_get_entry_name(entry, result_buf, result_len);
}
/* Take care of all ancestors first */
len = hp_get_function_stack(entry->prev_hprof, level - 1, result_buf, result_len);
/* Append the delimiter */
# define HP_STACK_DELIM "==>"
# define HP_STACK_DELIM_LEN (sizeof(HP_STACK_DELIM) - 1)
if (result_len < (len + HP_STACK_DELIM_LEN)) {
/* Insufficient result_buf. Bail out! */
return len;
}
/* Add delimiter only if entry had ancestors */
if (len) {
strncat(result_buf + len, HP_STACK_DELIM, result_len - len);
len += HP_STACK_DELIM_LEN;
}
# undef HP_STACK_DELIM_LEN
# undef HP_STACK_DELIM
/* Append the current function name */
return len + hp_get_entry_name(entry, result_buf + len, result_len - len);
}
/**
* Takes an input of the form /a/b/c/d/foo.php and returns
* a pointer to one-level directory and basefile name
* (d/foo.php) in the same string.
*/
static const char *hp_get_base_filename(const char *filename)
{
const char *ptr;
int found = 0;
if (!filename)
return "";
/* reverse search for "/" and return a ptr to the next char */
for (ptr = filename + strlen(filename) - 1; ptr >= filename; ptr--) {
if (*ptr == '/') {
found++;
}
if (found == 2) {
return ptr + 1;
}
}
/* no "/" char found, so return the whole string */
return filename;
}
/**
* Get the name of the current function. The name is qualified with
* the class name if the function is in a class.
*
* @author kannan, hzhao
*/
static zend_string *hp_get_function_name(zend_execute_data *execute_data)
{
zend_string *ret;
zend_function *curr_func;
zend_string *func = NULL;
if (!execute_data) {
return NULL;
}
/* shared meta data for function on the call stack */
curr_func = execute_data->func;
/* extract function name from the meta info */
func = curr_func->common.function_name;
if (!func) {
return NULL;
}
if (curr_func->common.scope != NULL) {
ret = strpprintf(0, "%s::%s", curr_func->common.scope->name->val, ZSTR_VAL(func));
} else {
ret = zend_string_init(ZSTR_VAL(func), ZSTR_LEN(func), 0);
}
return ret;
}
/**
* Free any items in the free list.
*/
static void hp_free_the_free_list()
{
hp_entry_t *p = XHPROF_G(entry_free_list);
hp_entry_t *cur;
while (p) {
cur = p;
p = p->prev_hprof;
free(cur);
}
}
/**
* Fast allocate a hp_entry_t structure. Picks one from the
* free list if available, else does an actual allocate.
*
* Doesn't bother initializing allocated memory.
*
* @author kannan
*/
static hp_entry_t *hp_fast_alloc_hprof_entry()
{
hp_entry_t *p;
p = XHPROF_G(entry_free_list);
if (p) {
XHPROF_G(entry_free_list) = p->prev_hprof;
return p;
} else {
return (hp_entry_t *)malloc(sizeof(hp_entry_t));
}
}
/**
* Fast free a hp_entry_t structure. Simply returns back
* the hp_entry_t to a free list and doesn't actually
* perform the free.
*
* @author kannan
*/
static void hp_fast_free_hprof_entry(hp_entry_t *p)
{
/* we use/overload the prev_hprof field in the structure to link entries in
* the free list.
* */
p->prev_hprof = XHPROF_G(entry_free_list);
XHPROF_G(entry_free_list) = p;
}
/**
* Increment the count of the given stat with the given count
* If the stat was not set before, inits the stat to the given count
*
* @param zval *counts Zend hash table pointer
* @param char *name Name of the stat
* @param long count Value of the stat to incr by
* @return void
* @author kannan
*/
void hp_inc_count(zval *counts, char *name, zend_long count)
{
HashTable *ht;
zval *data, val;
if (!counts) {
return;
}
ht = HASH_OF(counts);
if (!ht) {
return;
}
data = zend_hash_str_find(ht, name, strlen(name));
if (data) {
ZVAL_LONG(data, Z_LVAL_P(data) + count);
} else {
ZVAL_LONG(&val, count);
zend_hash_str_update(ht, name, strlen(name), &val);
}
}
/**
* Truncates the given timeval to the nearest slot begin, where
* the slot size is determined by intr
*
* @param tv Input timeval to be truncated in place
* @param intr Time interval in microsecs - slot width
* @return void
* @author veeve
*/
void hp_trunc_time(struct timeval *tv, zend_ulong intr)
{
zend_ulong time_in_micro;
/* Convert to microsecs and trunc that first */
time_in_micro = (tv->tv_sec * 1000000) + tv->tv_usec;
time_in_micro /= intr;
time_in_micro *= intr;
/* Update tv */
tv->tv_sec = (time_in_micro / 1000000);
tv->tv_usec = (time_in_micro % 1000000);
}
/**
* Sample the stack. Add it to the stats_count global.
*
* @param tv current time
* @param entries func stack as linked list of hp_entry_t
* @return void
* @author veeve
*/
void hp_sample_stack(hp_entry_t **entries)
{
char key[SCRATCH_BUF_LEN];
char symbol[SCRATCH_BUF_LEN * 1000];
/* Build key */
snprintf(key, sizeof(key), "%d.%06d", (uint32) XHPROF_G(last_sample_time).tv_sec, (uint32) XHPROF_G(last_sample_time).tv_usec);
/* Init stats in the global stats_count hashtable */
hp_get_function_stack(*entries, XHPROF_G(sampling_depth), symbol, sizeof(symbol));
add_assoc_string(&XHPROF_G(stats_count), key, symbol);
}
/**
* Checks to see if it is time to sample the stack.
* Calls hp_sample_stack() if its time.
*
* @param entries func stack as linked list of hp_entry_t
* @param last_sample time the last sample was taken
* @param sampling_intr sampling interval in microsecs
* @return void
* @author veeve
*/
void hp_sample_check(hp_entry_t **entries)
{
/* Validate input */
if (!entries || !(*entries)) {
return;
}
/* See if its time to sample. While loop is to handle a single function
* taking a long time and passing several sampling intervals. */
while ((cycle_timer() - XHPROF_G(last_sample_tsc)) > XHPROF_G(sampling_interval_tsc)) {
/* bump last_sample_tsc */
XHPROF_G(last_sample_tsc) += XHPROF_G(sampling_interval_tsc);
/* bump last_sample_time - HAS TO BE UPDATED BEFORE calling hp_sample_stack */
incr_us_interval(&XHPROF_G(last_sample_time), XHPROF_G(sampling_interval));
/* sample the stack */
hp_sample_stack(entries);
}
}
/**
* ***********************
* High precision timer related functions.
* ***********************
*/
static inline zend_ulong cycle_timer()
{
#if defined(__APPLE__) && defined(__MACH__)
return mach_absolute_time() / XHPROF_G(timebase_conversion);
#elif defined(ZEND_WIN32)
LARGE_INTEGER lt = {0};
QueryPerformanceCounter(<);
lt.QuadPart *= 1000000;
lt.QuadPart /= performance_frequency.QuadPart;
return lt.QuadPart;
#else
struct timespec s;
clock_gettime(CLOCK_MONOTONIC, &s);
return s.tv_sec * 1000 * 1000 + s.tv_nsec / 1000;
#endif
}
/**
* Get the current real CPU clock timer
*/
static zend_ulong cpu_timer()
{
#if defined(CLOCK_PROCESS_CPUTIME_ID)
struct timespec s;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &s);
return s.tv_sec * 1000 * 1000 + s.tv_nsec / 1000;
#else
struct rusage ru;
getrusage(RUSAGE_SELF, &ru);
return (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec ) * 1000 * 1000 + (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec);
#endif
}
/**
* Incr time with the given microseconds.
*/
static void incr_us_interval(struct timeval *start, zend_ulong incr)
{
incr += (start->tv_sec * 1000000 + start->tv_usec);
start->tv_sec = incr / 1000000;
start->tv_usec = incr % 1000000;
}
/**
* ***************************
* XHPROF DUMMY CALLBACKS
* ***************************
*/
void hp_mode_dummy_init_cb()
{
}
void hp_mode_dummy_exit_cb()
{
}
void hp_mode_dummy_beginfn_cb(hp_entry_t **entries, hp_entry_t *current)
{
}
void hp_mode_dummy_endfn_cb(hp_entry_t **entries)
{
}
/**
* ****************************
* XHPROF COMMON CALLBACKS
* ****************************
*/
/**
* XHPROF universal begin function.
* This function is called for all modes before the
* mode's specific begin_function callback is called.
*
* @param hp_entry_t **entries linked list (stack)
* of hprof entries
* @param hp_entry_t *current hprof entry for the current fn
* @return void
* @author kannan, veeve
*/
void hp_mode_common_beginfn(hp_entry_t **entries, hp_entry_t *current)
{
hp_entry_t *p;
/* This symbol's recursive level */
int recurse_level = 0;
if (XHPROF_G(func_hash_counters[current->hash_code]) > 0) {
/* Find this symbols recurse level */
for (p = (*entries); p; p = p->prev_hprof) {
if (zend_string_equals(current->name_hprof, p->name_hprof)) {
recurse_level = (p->rlvl_hprof) + 1;
break;
}
}
}
XHPROF_G(func_hash_counters[current->hash_code])++;
/* Init current function's recurse level */
current->rlvl_hprof = recurse_level;
}
/**
* *********************************
* XHPROF INIT MODULE CALLBACKS
* *********************************
*/
/**
* XHPROF_MODE_SAMPLED's init callback
*
* @author veeve
*/
void hp_mode_sampled_init_cb()
{
/* Init the last_sample in tsc */
XHPROF_G(last_sample_tsc) = cycle_timer();
/* Find the microseconds that need to be truncated */
gettimeofday(&XHPROF_G(last_sample_time), 0);
hp_trunc_time(&XHPROF_G(last_sample_time), XHPROF_G(sampling_interval));
/* Convert sampling interval to ticks */
XHPROF_G(sampling_interval_tsc) = XHPROF_G(sampling_interval);
}