2017-02-21 11:18:15 +03:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
debug_counter.c -
|
|
|
|
|
|
|
|
created at: Tue Feb 21 16:51:18 2017
|
|
|
|
|
|
|
|
Copyright (C) 2017 Koichi Sasada
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
2019-12-24 19:32:37 +03:00
|
|
|
#include "debug_counter.h"
|
2020-12-16 19:29:15 +03:00
|
|
|
#include "internal.h"
|
2018-09-25 21:13:29 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <locale.h>
|
2020-12-16 19:29:15 +03:00
|
|
|
#include "ruby/thread_native.h"
|
2017-02-21 11:18:15 +03:00
|
|
|
|
2019-08-07 17:22:06 +03:00
|
|
|
#if USE_DEBUG_COUNTER
|
2020-12-16 19:29:15 +03:00
|
|
|
|
2022-12-08 04:09:30 +03:00
|
|
|
const char *const rb_debug_counter_names[] = {
|
2022-07-02 16:41:19 +03:00
|
|
|
#define DEBUG_COUNTER_NAME_EMPTY "" /* Suppress -Wstring-concatenation */
|
|
|
|
DEBUG_COUNTER_NAME_EMPTY
|
|
|
|
#undef DEBUG_COUNTER_NAME_EMPTY
|
2017-03-10 10:18:03 +03:00
|
|
|
#define RB_DEBUG_COUNTER(name) #name,
|
|
|
|
#include "debug_counter.h"
|
|
|
|
#undef RB_DEBUG_COUNTER
|
2017-02-21 11:18:15 +03:00
|
|
|
};
|
|
|
|
|
2019-03-29 16:54:29 +03:00
|
|
|
MJIT_SYMBOL_EXPORT_BEGIN
|
2022-12-08 04:09:30 +03:00
|
|
|
size_t rb_debug_counter[numberof(rb_debug_counter_names)];
|
2020-12-16 19:29:15 +03:00
|
|
|
void rb_debug_counter_add_atomic(enum rb_debug_counter_type type, int add);
|
2019-03-29 16:54:29 +03:00
|
|
|
MJIT_SYMBOL_EXPORT_END
|
2017-02-21 11:18:15 +03:00
|
|
|
|
2022-07-02 17:56:11 +03:00
|
|
|
static rb_nativethread_lock_t debug_counter_lock;
|
2020-12-16 19:29:15 +03:00
|
|
|
|
|
|
|
__attribute__((constructor))
|
|
|
|
static void
|
|
|
|
debug_counter_setup(void)
|
|
|
|
{
|
|
|
|
rb_nativethread_lock_initialize(&debug_counter_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_debug_counter_add_atomic(enum rb_debug_counter_type type, int add)
|
|
|
|
{
|
|
|
|
rb_nativethread_lock_lock(&debug_counter_lock);
|
|
|
|
{
|
|
|
|
rb_debug_counter[(int)type] += add;
|
|
|
|
}
|
|
|
|
rb_nativethread_lock_unlock(&debug_counter_lock);
|
|
|
|
}
|
|
|
|
|
2022-07-02 17:56:11 +03:00
|
|
|
static int debug_counter_disable_show_at_exit = 0;
|
2019-12-24 19:32:37 +03:00
|
|
|
|
|
|
|
// note that this operation is not atomic.
|
|
|
|
void
|
|
|
|
ruby_debug_counter_reset(void)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < RB_DEBUG_COUNTER_MAX; i++) {
|
2022-12-07 10:01:30 +03:00
|
|
|
rb_debug_counter[i] = 0;
|
2019-12-24 19:32:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// note that this operation is not atomic.
|
|
|
|
size_t
|
|
|
|
ruby_debug_counter_get(const char **names_ptr, size_t *counters_ptr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (names_ptr != NULL) {
|
|
|
|
for (i=0; i<RB_DEBUG_COUNTER_MAX; i++) {
|
2022-12-08 04:09:30 +03:00
|
|
|
names_ptr[i] = rb_debug_counter_names[i];
|
2019-12-24 19:32:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (counters_ptr != NULL) {
|
|
|
|
for (i=0; i<RB_DEBUG_COUNTER_MAX; i++) {
|
|
|
|
counters_ptr[i] = rb_debug_counter[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return RB_DEBUG_COUNTER_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ruby_debug_counter_show_at_exit(int enable)
|
|
|
|
{
|
|
|
|
debug_counter_disable_show_at_exit = !enable;
|
|
|
|
}
|
|
|
|
|
2018-09-25 21:13:29 +03:00
|
|
|
void
|
|
|
|
rb_debug_counter_show_results(const char *msg)
|
2017-02-21 11:18:15 +03:00
|
|
|
{
|
|
|
|
const char *env = getenv("RUBY_DEBUG_COUNTER_DISABLE");
|
2018-09-25 21:13:29 +03:00
|
|
|
|
|
|
|
setlocale(LC_NUMERIC, "");
|
|
|
|
|
2017-02-21 11:18:15 +03:00
|
|
|
if (env == NULL || strcmp("1", env) != 0) {
|
|
|
|
int i;
|
2018-09-25 21:13:29 +03:00
|
|
|
fprintf(stderr, "[RUBY_DEBUG_COUNTER]\t%d %s\n", getpid(), msg);
|
2017-02-21 11:18:15 +03:00
|
|
|
for (i=0; i<RB_DEBUG_COUNTER_MAX; i++) {
|
2018-09-28 04:10:43 +03:00
|
|
|
fprintf(stderr, "[RUBY_DEBUG_COUNTER]\t%-30s\t%'14"PRIuSIZE"\n",
|
2022-12-08 04:09:30 +03:00
|
|
|
rb_debug_counter_names[i],
|
2017-02-21 11:18:15 +03:00
|
|
|
rb_debug_counter[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 22:23:30 +03:00
|
|
|
VALUE
|
Fix rb_define_singleton_method warning
for debug counters
```
../include/ruby/intern.h:1175:137: warning: passing argument 3 of 'rb_define_singleton_method0' from incompatible pointer type [-Wincompatible-pointer-types]
#define rb_define_singleton_method(klass, mid, func, arity) rb_define_singleton_method_choose_prototypem3((arity),(func))((klass),(mid),(func),(arity));
^
../vm.c:2958:5: note: in expansion of macro 'rb_define_singleton_method'
rb_define_singleton_method(rb_cRubyVM, "show_debug_counters", rb_debug_counter_show, 0);
^~~~~~~~~~~~~~~~~~~~~~~~~~
../include/ruby/intern.h:1139:99: note: expected 'VALUE (*)(VALUE) {aka long unsigned int (*)(long unsigned int)}' but argument is of type 'VALUE (*)(void) {aka long unsigned int (*)(void)}'
__attribute__((__unused__,__weakref__("rb_define_singleton_method"),__nonnull__(2,3)))static void rb_define_singleton_method0 (VALUE,const char*,VALUE(*)(VALUE),int);
```
2019-09-20 11:44:16 +03:00
|
|
|
rb_debug_counter_show(RB_UNUSED_VAR(VALUE klass))
|
2019-08-06 22:23:30 +03:00
|
|
|
{
|
2020-03-15 12:25:46 +03:00
|
|
|
rb_debug_counter_show_results("show_debug_counters");
|
|
|
|
ruby_debug_counter_show_at_exit(FALSE);
|
2019-08-06 22:23:30 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2019-04-14 09:57:21 +03:00
|
|
|
VALUE
|
Fix rb_define_singleton_method warning
for debug counters
```
../include/ruby/intern.h:1175:137: warning: passing argument 3 of 'rb_define_singleton_method0' from incompatible pointer type [-Wincompatible-pointer-types]
#define rb_define_singleton_method(klass, mid, func, arity) rb_define_singleton_method_choose_prototypem3((arity),(func))((klass),(mid),(func),(arity));
^
../vm.c:2958:5: note: in expansion of macro 'rb_define_singleton_method'
rb_define_singleton_method(rb_cRubyVM, "show_debug_counters", rb_debug_counter_show, 0);
^~~~~~~~~~~~~~~~~~~~~~~~~~
../include/ruby/intern.h:1139:99: note: expected 'VALUE (*)(VALUE) {aka long unsigned int (*)(long unsigned int)}' but argument is of type 'VALUE (*)(void) {aka long unsigned int (*)(void)}'
__attribute__((__unused__,__weakref__("rb_define_singleton_method"),__nonnull__(2,3)))static void rb_define_singleton_method0 (VALUE,const char*,VALUE(*)(VALUE),int);
```
2019-09-20 11:44:16 +03:00
|
|
|
rb_debug_counter_reset(RB_UNUSED_VAR(VALUE klass))
|
2019-04-14 09:57:21 +03:00
|
|
|
{
|
2019-12-24 19:32:37 +03:00
|
|
|
ruby_debug_counter_reset();
|
2019-04-14 09:57:21 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2018-09-25 21:13:29 +03:00
|
|
|
__attribute__((destructor))
|
|
|
|
static void
|
|
|
|
debug_counter_show_results_at_exit(void)
|
|
|
|
{
|
2019-12-24 19:32:37 +03:00
|
|
|
if (debug_counter_disable_show_at_exit == 0) {
|
|
|
|
rb_debug_counter_show_results("normal exit.");
|
|
|
|
}
|
2018-09-25 21:13:29 +03:00
|
|
|
}
|
2020-12-16 19:29:15 +03:00
|
|
|
|
2018-09-25 21:13:29 +03:00
|
|
|
#else
|
2020-12-16 19:29:15 +03:00
|
|
|
|
2018-09-25 21:13:29 +03:00
|
|
|
void
|
|
|
|
rb_debug_counter_show_results(const char *msg)
|
|
|
|
{
|
|
|
|
}
|
2019-12-24 19:32:37 +03:00
|
|
|
|
|
|
|
size_t
|
|
|
|
ruby_debug_counter_get(const char **names_ptr, size_t *counters_ptr)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void
|
|
|
|
ruby_debug_counter_reset(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ruby_debug_counter_show_at_exit(int enable)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-21 11:18:15 +03:00
|
|
|
#endif /* USE_DEBUG_COUNTER */
|