appoptics_apm 4.11.2 → 4.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/build_and_release_gem.yml +103 -0
  3. data/.github/workflows/build_for_packagecloud.yml +70 -0
  4. data/.github/workflows/docker-images.yml +47 -0
  5. data/.github/workflows/run_cpluplus_tests.yml +73 -0
  6. data/.github/workflows/run_tests.yml +168 -0
  7. data/.github/workflows/scripts/test_install.rb +23 -0
  8. data/.github/workflows/swig/swig-v4.0.2.tar.gz +0 -0
  9. data/.github/workflows/test_on_4_linux.yml +159 -0
  10. data/.gitignore +26 -25
  11. data/.travis.yml +30 -23
  12. data/Gemfile +2 -26
  13. data/LICENSE +202 -193
  14. data/README.md +6 -6
  15. data/appoptics_apm.gemspec +15 -8
  16. data/examples/prepend.rb +13 -0
  17. data/examples/sdk_examples.rb +16 -0
  18. data/ext/oboe_metal/README.md +2 -2
  19. data/ext/oboe_metal/extconf.rb +45 -22
  20. data/ext/oboe_metal/lib/liboboe-1.0-alpine-x86_64.so.0.0.0.sha256 +1 -0
  21. data/ext/oboe_metal/lib/liboboe-1.0-x86_64.so.0.0.0.sha256 +1 -0
  22. data/ext/oboe_metal/src/README.md +6 -0
  23. data/ext/oboe_metal/src/VERSION +2 -1
  24. data/ext/oboe_metal/src/frames.cc +246 -0
  25. data/ext/oboe_metal/src/frames.h +40 -0
  26. data/ext/oboe_metal/src/init_appoptics_apm.cc +21 -0
  27. data/ext/oboe_metal/src/logging.cc +95 -0
  28. data/ext/oboe_metal/src/logging.h +35 -0
  29. data/ext/oboe_metal/src/oboe.h +46 -23
  30. data/ext/oboe_metal/src/oboe_api.cpp +652 -0
  31. data/ext/oboe_metal/src/oboe_api.hpp +431 -0
  32. data/ext/oboe_metal/src/oboe_debug.h +2 -0
  33. data/ext/oboe_metal/src/{oboe_wrap.cxx → oboe_swig_wrap.cc} +1523 -1327
  34. data/ext/oboe_metal/src/profiling.cc +435 -0
  35. data/ext/oboe_metal/src/profiling.h +78 -0
  36. data/ext/oboe_metal/test/CMakeLists.txt +53 -0
  37. data/ext/oboe_metal/test/FindGMock.cmake +43 -0
  38. data/ext/oboe_metal/test/README.md +56 -0
  39. data/ext/oboe_metal/test/frames_test.cc +164 -0
  40. data/ext/oboe_metal/test/profiling_test.cc +93 -0
  41. data/ext/oboe_metal/test/ruby_inc_dir.rb +8 -0
  42. data/ext/oboe_metal/test/ruby_prefix.rb +8 -0
  43. data/ext/oboe_metal/test/ruby_test_helper.rb +67 -0
  44. data/ext/oboe_metal/test/test.h +11 -0
  45. data/ext/oboe_metal/test/test_main.cc +32 -0
  46. data/lib/appoptics_apm/api/metrics.rb +7 -1
  47. data/lib/appoptics_apm/base.rb +1 -1
  48. data/lib/appoptics_apm/config.rb +20 -10
  49. data/lib/appoptics_apm/frameworks/rails/inst/action_view.rb +64 -21
  50. data/lib/appoptics_apm/frameworks/rails/inst/connection_adapters/utils5x.rb +7 -1
  51. data/lib/appoptics_apm/frameworks/rails.rb +7 -1
  52. data/lib/appoptics_apm/inst/http.rb +67 -59
  53. data/lib/appoptics_apm/inst/rack.rb +13 -6
  54. data/lib/appoptics_apm/inst/rack_cache.rb +35 -0
  55. data/lib/appoptics_apm/inst/redis.rb +1 -2
  56. data/lib/appoptics_apm/noop/context.rb +4 -3
  57. data/lib/appoptics_apm/noop/metadata.rb +4 -1
  58. data/lib/appoptics_apm/noop/profiling.rb +21 -0
  59. data/lib/appoptics_apm/oboe_init_options.rb +41 -23
  60. data/lib/appoptics_apm/sdk/current_trace.rb +5 -9
  61. data/lib/appoptics_apm/support/profiling.rb +18 -0
  62. data/lib/appoptics_apm/support/transaction_metrics.rb +1 -1
  63. data/lib/appoptics_apm/support/transaction_settings.rb +3 -3
  64. data/lib/appoptics_apm/support/x_trace_options.rb +2 -2
  65. data/lib/appoptics_apm/support_report.rb +2 -2
  66. data/lib/appoptics_apm/test.rb +5 -4
  67. data/lib/appoptics_apm/util.rb +4 -3
  68. data/lib/appoptics_apm/version.rb +4 -3
  69. data/lib/appoptics_apm/xtrace.rb +1 -1
  70. data/lib/appoptics_apm.rb +4 -2
  71. data/lib/oboe_metal.rb +2 -2
  72. data/lib/rails/generators/appoptics_apm/templates/appoptics_apm_initializer.rb +80 -20
  73. data/log/.keep +0 -0
  74. metadata +47 -14
  75. data/.travis/bundle.sh +0 -9
  76. data/ext/oboe_metal/src/oboe.hpp +0 -874
data/README.md CHANGED
@@ -12,11 +12,11 @@ It requires an [AppOptics](https://www.appoptics.com/) account to view metrics.
12
12
  [it's free](https://my.appoptics.com/sign_up).
13
13
 
14
14
  [![Gem Version](https://badge.fury.io/rb/appoptics_apm.svg)](https://badge.fury.io/rb/appoptics_apm)
15
- [![Build Status](https://travis-ci.org/appoptics/appoptics-apm-ruby.png?branch=master)](https://travis-ci.org/appoptics/appoptics-apm-ruby)
16
- [![Maintainability](https://api.codeclimate.com/v1/badges/ac7f36241a23a3a82fc5/maintainability)](https://codeclimate.com/github/appoptics/appoptics-apm-ruby/maintainability)
17
15
 
18
- _Note: The repository is now at https://github.com/appoptics/appoptics-apm-ruby Please update your github remotes with
19
- `git remote set-url origin git@github.com:appoptics/appoptics-apm-ruby.git`._
16
+ [![Run all Tests](https://github.com/appoptics/appoptics-apm-ruby/actions/workflows/run_tests.yml/badge.svg)](https://github.com/appoptics/appoptics-apm-ruby/actions/workflows/run_tests.yml)
17
+ [![C++ Tests](https://github.com/appoptics/appoptics-apm-ruby/actions/workflows/run_cpluplus_tests.yml/badge.svg)](https://github.com/appoptics/appoptics-apm-ruby/actions/workflows/run_cpluplus_tests.yml)
18
+
19
+ [comment]: <> ([![Maintainability]&#40;https://api.codeclimate.com/v1/badges/ac7f36241a23a3a82fc5/maintainability&#41;]&#40;https://codeclimate.com/github/appoptics/appoptics-apm-ruby/maintainability&#41;)
20
20
 
21
21
  # Documentation
22
22
 
@@ -234,7 +234,7 @@ Find more details in the [RubyDoc page](https://www.rubydoc.info/gems/appoptics_
234
234
  # Support
235
235
 
236
236
  If you run into a problem, find a bug, or would like to request an enhancement, feel free to contact our tech support
237
- [support@appoptics.com](support@appoptics.com).
237
+ [technicalsupport@solarwinds.com](technicalsupport@solarwinds.com).
238
238
 
239
239
  # Contributing
240
240
 
@@ -390,4 +390,4 @@ See the README in the test directory.
390
390
 
391
391
  Copyright (c) 2018 SolarWinds, LLC
392
392
 
393
- Released under the [Librato Open License](https://docs.appoptics.com/kb/apm_tracing/librato-open-license/)
393
+ Released under the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0)
@@ -6,10 +6,10 @@ Gem::Specification.new do |s|
6
6
  s.version = AppOpticsAPM::Version::STRING
7
7
  s.date = Time.now.strftime('%Y-%m-%d')
8
8
 
9
- s.license = "Librato Open License, Version 1.0, https://bit.ly/2Kmm0mN"
9
+ s.license = "Apache-2.0"
10
10
 
11
11
  s.authors = ["Maia Engeli", "Peter Giacomo Lombardo", "Spiros Eliopoulos"]
12
- s.email = %q{support@appoptics.com}
12
+ s.email = %q{technicalsupport@solarwinds.com}
13
13
  s.homepage = %q{https://www.appoptics.com/}
14
14
  s.summary = %q{AppOptics APM performance instrumentation gem for Ruby}
15
15
  s.description = <<-EOF
@@ -26,12 +26,16 @@ Automatic tracing and metrics for Ruby applications. Get started at appoptics.co
26
26
  s.extra_rdoc_files = ['LICENSE']
27
27
  s.files = `git ls-files`.split("\n").reject { |f| f.match(%r{^(test|gemfiles)/}) }
28
28
  s.files += ['ext/oboe_metal/src/oboe.h',
29
- 'ext/oboe_metal/src/oboe.hpp',
29
+ 'ext/oboe_metal/src/oboe_api.cpp',
30
+ 'ext/oboe_metal/src/oboe_api.hpp',
30
31
  'ext/oboe_metal/src/oboe_debug.h',
31
- 'ext/oboe_metal/src/oboe_wrap.cxx',
32
+ 'ext/oboe_metal/src/oboe_swig_wrap.cc',
32
33
  'ext/oboe_metal/src/bson/bson.h',
33
34
  'ext/oboe_metal/src/bson/platform_hacks.h',
34
- 'ext/oboe_metal/src/VERSION']
35
+ 'ext/oboe_metal/src/VERSION',
36
+ 'ext/oboe_metal/lib/liboboe-1.0-alpine-x86_64.so.0.0.0.sha256',
37
+ 'ext/oboe_metal/lib/liboboe-1.0-x86_64.so.0.0.0.sha256'
38
+ ]
35
39
  s.files -= ['build_gem.sh',
36
40
  'build_gem_upload_to_packagecloud.sh',
37
41
  'Rakefile']
@@ -42,17 +46,20 @@ Automatic tracing and metrics for Ruby applications. Get started at appoptics.co
42
46
 
43
47
  s.extensions = ['ext/oboe_metal/extconf.rb'] unless defined?(JRUBY_VERSION)
44
48
 
45
- s.add_runtime_dependency('json', '~> 2.3')
49
+ # this still gives a warning, would have to be pinned to a minor version
50
+ # but that is not necessary and may restrict other gems
51
+ s.add_runtime_dependency('json', '~> 2.0')
46
52
  s.add_runtime_dependency('no_proxy_fix', '~> 0.1.2', '>= 0.1.2')
47
53
 
48
54
  # Development dependencies used in gem development & testing
49
55
  # s.add_development_dependency('rake', '>= 0.9.0')
50
56
  # s.add_development_dependency('simplecov', '>= 0.16.0') if ENV["SIMPLECOV_COVERAGE"]
51
57
  # s.add_development_dependency('simplecov-console', '>= 0.4.0') if ENV["SIMPLECOV_COVERAGE"]
52
- # s.add_development_dependency('irb', '>= 1.0.0') if RUBY_VERSION >= '2.6.0'
58
+ # s.add_development_dependency('irb', '>= 1.0.0', '< 1.2.2') if RUBY_VERSION >= '2.6.0'
53
59
  #
54
60
  # unless defined?(JRUBY_VERSION)
55
- # s.add_development_dependency('byebug', '>= 8.0.0')
61
+ # s.add_development_dependency('byebug', '>= 11.0.0')
62
+ # s.add_development_dependency('minitest')
56
63
  # s.add_development_dependency('minitest-hooks', '>= 1.5.0')
57
64
  # s.add_development_dependency('minitest-focus', '>=1.1.2')
58
65
  # s.add_development_dependency('benchmark-ips', '>= 2.7.2')
@@ -0,0 +1,13 @@
1
+ require 'appoptics_apm'
2
+
3
+ module Measurements
4
+ def request(*args, &block)
5
+ req = args.first
6
+ AppOpticsAPM::SDK.summary_metric("request_size", req.to_hash.to_s.size)
7
+ resp = super
8
+ AppOpticsAPM::SDK.summary_metric("response_size", resp.to_hash.to_s.size)
9
+ return resp
10
+ end
11
+ end
12
+
13
+ Net::HTTP.send(:prepend, :Measurements)
@@ -140,3 +140,19 @@ AppOpticsAPM::SDK.start_trace('log_trace_id') do
140
140
  trace = AppOpticsAPM::SDK.current_trace
141
141
  AppOpticsAPM.logger.warn "Some log message #{trace.for_log}"
142
142
  end
143
+
144
+ ###############################################################
145
+ # START A TRACE AND PROFILE
146
+ ###############################################################
147
+ #
148
+ # AppOpticsAPM::Profiling.run
149
+ # This method adds profiling for the code executed in the block
150
+
151
+ AppOpticsAPM::SDK.start_trace("#{name}_profiling") do
152
+ AppOpticsAPM::Profiling.run do
153
+ 10.times do
154
+ [9, 6, 12, 2, 7, 1, 9, 3, 4, 14, 5, 8].sort
155
+ sleep 0.2
156
+ end
157
+ end
158
+ end
@@ -5,7 +5,7 @@ inspired by: https://dev.to/wataash/how-to-create-and-debug-ruby-gem-with-c-nati
5
5
 
6
6
  ## install ruby with sources
7
7
 
8
- rbenv is your friend ;) 😄 -k means keep sources
8
+ rbenv is your friend ;) -k means keep sources
9
9
 
10
10
  ```
11
11
  rbenv install -k 2.6.3
@@ -66,4 +66,4 @@ Some inspiring examples here:
66
66
 
67
67
  https://jvns.ca/blog/2016/06/12/a-weird-system-call-process-vm-readv/
68
68
 
69
- https://medium.com/@zanker/finding-a-ruby-bug-with-gdb-56d6b321bc86
69
+ https://medium.com/@zanker/finding-a-ruby-bug-with-gdb-56d6b321bc86
@@ -8,20 +8,29 @@ require 'rbconfig'
8
8
  require 'open-uri'
9
9
  require 'no_proxy_fix'
10
10
 
11
+ CONFIG['warnflags'] = CONFIG['warnflags'].gsub(/-Wdeclaration-after-statement/, '')
12
+ .gsub(/-Wimplicit-function-declaration/, '')
13
+ .gsub(/-Wimplicit-int/, '')
14
+ .gsub(/-Wno-tautological-compare/, '')
15
+ .gsub(/-Wno-self-assign/, '')
16
+ .gsub(/-Wno-parentheses-equality/, '')
17
+ .gsub(/-Wno-constant-logical-operand/, '')
18
+ .gsub(/-Wno-cast-function-type/, '')
19
+ init_mkmf(CONFIG)
20
+
11
21
  ext_dir = File.expand_path(File.dirname(__FILE__))
12
22
 
13
23
  # Check if we're running in JRuby
14
24
  jruby = defined?(JRUBY_VERSION) ? true : false
15
-
16
25
  # Set the mkmf lib paths so we have no issues linking to
17
26
  # the AppOpticsAPM libs.
18
27
  ao_lib_dir = File.join(ext_dir, 'lib')
19
28
  ao_include = File.join(ext_dir, 'src')
20
29
 
21
30
  # Download the appropriate liboboe from S3(via rake for testing) or files.appoptics.com (production)
22
- version = File.read(File.join(ao_include, 'VERSION')).chomp
31
+ version = File.read(File.join(ao_include, 'VERSION')).strip
23
32
  if ENV['APPOPTICS_FROM_S3'].to_s.downcase == 'true'
24
- ao_path = File.join('https://s3-us-west-2.amazonaws.com/rc-files-t2/c-lib/', version)
33
+ ao_path = File.join('https://rc-files-t2.s3-us-west-2.amazonaws.com/c-lib/', version)
25
34
  puts 'Fetching c-lib from S3'
26
35
  else
27
36
  ao_path = File.join('https://files.appoptics.com/c-lib', version)
@@ -29,7 +38,8 @@ end
29
38
 
30
39
  ao_arch = 'x86_64'
31
40
  if File.exist?('/etc/alpine-release')
32
- version = open('/etc/alpine-release').read.chomp
41
+ version = File.read('/etc/alpine-release').strip
42
+
33
43
  ao_arch =
34
44
  if Gem::Version.new(version) < Gem::Version.new('3.9')
35
45
  'alpine-libressl-x86_64'
@@ -40,31 +50,31 @@ end
40
50
 
41
51
  ao_clib = "liboboe-1.0-#{ao_arch}.so.0.0.0"
42
52
  ao_item = File.join(ao_path, ao_clib)
43
- ao_checksum_item = "#{ao_item}.sha256"
53
+ ao_checksum_file = File.join(ao_lib_dir, "#{ao_clib}.sha256")
44
54
  clib = File.join(ao_lib_dir, ao_clib)
45
55
 
46
56
  retries = 3
47
57
  success = false
48
58
  while retries > 0
49
59
  begin
50
- # download
51
- # TODO warning: calling URI.open via Kernel#open is deprecated, call URI.open directly or use URI#open
52
- # ____ URI.open is not supported in 2.4.5, let's use open until we can deprecate 2.4.5
53
- download = open(ao_item, 'rb')
60
+ download = RUBY_VERSION < '2.5.0' ? open(ao_item, 'rb') : URI.open(ao_item, 'rb')
54
61
  IO.copy_stream(download, clib)
55
62
 
56
- checksum = open(ao_checksum_item, 'r').read.chomp
57
63
  clib_checksum = Digest::SHA256.file(clib).hexdigest
64
+ download.close
65
+ checksum = File.read(ao_checksum_file).strip
58
66
 
59
- # verify_checksum
67
+ # unfortunately these messages only show if the install command is run
68
+ # with the `--verbose` flag
60
69
  if clib_checksum != checksum
61
70
  $stderr.puts '== ERROR ================================================================='
62
- $stderr.puts 'Checksum Verification failed for the c-extension of the appoptics_apm gem.'
63
- $stderr.puts 'appoptics_apm will not instrument the code. No tracing will occur.'
64
- $stderr.puts 'Contact support@appoptics.com if the problem persists.'
71
+ $stderr.puts 'Checksum Verification failed for the c-extension of the appoptics_apm gem'
72
+ $stderr.puts 'Installation cannot continue'
73
+ $stderr.puts "\nChecksum packaged with gem: #{checksum}"
74
+ $stderr.puts "Checksum calculated from lib: #{clib_checksum}"
75
+ $stderr.puts 'Contact technicalsupport@solarwinds.com if the problem persists'
65
76
  $stderr.puts '=========================================================================='
66
- create_makefile('oboe_noop', 'noop')
67
- retries = 0
77
+ exit 1
68
78
  else
69
79
  success = true
70
80
  retries = 0
@@ -76,8 +86,8 @@ while retries > 0
76
86
  $stderr.puts '== ERROR =========================================================='
77
87
  $stderr.puts 'Download of the c-extension for the appoptics_apm gem failed.'
78
88
  $stderr.puts 'appoptics_apm will not instrument the code. No tracing will occur.'
79
- $stderr.puts 'Contact support@appoptics.com if the problem persists.'
80
- $stderr.puts "error:\n#{e.message}"
89
+ $stderr.puts 'Contact technicalsupport@solarwinds.com if the problem persists.'
90
+ $stderr.puts "error: #{ao_item}\n#{e.message}"
81
91
  $stderr.puts '==================================================================='
82
92
  create_makefile('oboe_noop', 'noop')
83
93
  end
@@ -107,11 +117,24 @@ if success
107
117
  $libs = append_library($libs, 'stdc++')
108
118
 
109
119
  $CFLAGS << " #{ENV['CFLAGS']}"
110
- $CPPFLAGS << " #{ENV['CPPFLAGS']}"
120
+ # $CPPFLAGS << " #{ENV['CPPFLAGS']} -std=c++11"
121
+ # TODO for debugging: -pg -gdwarf-2, remove for production
122
+ # -pg does not work on alpine https://www.openwall.com/lists/musl/2014/11/05/2
123
+ $CPPFLAGS << " #{ENV['CPPFLAGS']} -std=c++11 -gdwarf-2 -I$$ORIGIN/../ext/oboe_metal/include -I$$ORIGIN/../ext/oboe_metal/src"
124
+ # $CPPFLAGS << " #{ENV['CPPFLAGS']} -std=c++11 -I$$ORIGIN/../ext/oboe_metal/include"
111
125
  $LIBS << " #{ENV['LIBS']}"
112
- $LDFLAGS << " #{ENV['LDFLAGS']} '-Wl,-rpath=$$ORIGIN/../ext/oboe_metal/lib'"
113
126
 
114
- create_makefile('oboe_metal', 'src')
127
+ # use "z,defs" to see what happens during linking
128
+ # $LDFLAGS << " #{ENV['LDFLAGS']} '-Wl,-rpath=$$ORIGIN/../ext/oboe_metal/lib,-z,defs' -lrt"
129
+ $LDFLAGS << " #{ENV['LDFLAGS']} '-Wl,-rpath=$$ORIGIN/../ext/oboe_metal/lib' -lrt"
130
+ $CXXFLAGS += " -std=c++11 "
131
+
132
+ # ____ include debug info, comment out when not debugging
133
+ # ____ -pg -> profiling info for gprof
134
+ CONFIG["debugflags"] = "-ggdb3 "
135
+ CONFIG["optflags"] = "-O0"
136
+
137
+ create_makefile('libappoptics_apm', 'src')
115
138
 
116
139
  else
117
140
  $stderr.puts '== ERROR ========================================================='
@@ -121,7 +144,7 @@ if success
121
144
  else
122
145
  $stderr.puts 'Could not find a matching c-library. No tracing will occur.'
123
146
  end
124
- $stderr.puts 'Contact support@appoptics.com if the problem persists.'
147
+ $stderr.puts 'Contact technicalsupport@solarwinds.com if the problem persists.'
125
148
  $stderr.puts '=================================================================='
126
149
  create_makefile('oboe_noop', 'noop')
127
150
  end
@@ -0,0 +1 @@
1
+ af7d764a88cec323940fc6040947f2dcf8041c389744bdfc46b7f2fe0a465dab
@@ -0,0 +1 @@
1
+ 092e232d0e1aa1826b77c98d96f7bb2db39058c9bec3e2c0387cc6fb51b5b83c
@@ -0,0 +1,6 @@
1
+
2
+ oboe_api.hpp and oboe_api.cpp are copied here from the oboe repo during install
3
+
4
+ it is best to edit these file in the oboe repo and use the OBOE_WIP=true env
5
+ var to include the development version while they aren't deploy to s3.
6
+
@@ -1 +1,2 @@
1
- 10.0.0
1
+ 10.2.0
2
+
@@ -0,0 +1,246 @@
1
+ // Copyright (c) 2021 SolarWinds, LLC.
2
+ // All rights reserved.
3
+
4
+ #include "frames.h"
5
+
6
+ using namespace std;
7
+
8
+ unordered_map<VALUE, FrameData> cached_frames;
9
+
10
+ // in theory the mutex is not needed, because Ruby does not context switch
11
+ // while executing a foreign function, but will this always hold true?
12
+ mutex cached_frames_mutex;
13
+
14
+ void Frames::reserve_cached_frames() {
15
+ lock_guard<mutex> guard(cached_frames_mutex);
16
+ // unordered_maps grow automatically, but it starts at 1 and then
17
+ // doubles when it is full, so lets avoid the warmup
18
+ cached_frames.reserve(500); // it will round to a prime number: 503
19
+ }
20
+
21
+ void Frames::clear_cached_frames() {
22
+ lock_guard<mutex> guard(cached_frames_mutex);
23
+ // unordered_maps grow automatically, but it starts at 1 and then
24
+ // doubles when it is full, so lets avoid the warmup
25
+ cached_frames.clear();
26
+ }
27
+
28
+ // this is a private function
29
+ int Frames::cache_frame(VALUE frame) {
30
+ VALUE val;
31
+ FrameData data;
32
+
33
+ // only cache it if it does not exist
34
+ if (cached_frames.count(frame) == 0) {
35
+ val = rb_profile_frame_label(frame); // returns method or block
36
+ if (RB_TYPE_P(val, T_STRING))
37
+ data.method = RSTRING_PTR(val);
38
+
39
+ if (data.method.rfind("block ", 0) == 0) {
40
+ // we don't need more info if it is a block
41
+ // we ignore block level info because they make things messy
42
+ lock_guard<mutex> guard(cached_frames_mutex);
43
+ cached_frames.insert({frame, data});
44
+ return 0;
45
+ }
46
+
47
+ val = rb_profile_frame_classpath(frame); // returns class or nil
48
+ if (RB_TYPE_P(val, T_STRING)) data.klass = RSTRING_PTR(val);
49
+
50
+ val = rb_profile_frame_absolute_path(frame); // returns file, use rb_profile_frame_path() if nil
51
+ if (!RB_TYPE_P(val, T_STRING)) val = rb_profile_frame_path(frame);
52
+ if (RB_TYPE_P(val, T_STRING)) data.file = RSTRING_PTR(val);
53
+
54
+ // Ruby 3 reports <cfunc>, but the linenumbers are bogus
55
+ // the default line number is 0
56
+ if (!data.file.compare("<cfunc>") == 0) {
57
+ val = rb_profile_frame_first_lineno(frame); // returns line number
58
+ if (RB_TYPE_P(val, T_FIXNUM)) {
59
+ data.lineno = NUM2INT(val);
60
+ }
61
+ }
62
+ lock_guard<mutex> guard(cached_frames_mutex);
63
+ cached_frames.insert({frame, data});
64
+ }
65
+ return 0;
66
+ }
67
+
68
+ // all frames in frames_buffer must be in cached_frames
69
+ // before calling this function
70
+ // we are saving the check for better performance
71
+ int Frames::collect_frame_data(VALUE *frames_buffer, int num, vector<FrameData> &frame_data) {
72
+ if (num == 1) {
73
+ if (frames_buffer[0] == PR_IN_GC) {
74
+ FrameData data;
75
+ data.method = "GARBAGE COLLECTION";
76
+ frame_data.push_back(data);
77
+ return 0;
78
+ } else if (frames_buffer[0] == PR_OTHER_THREAD) {
79
+ FrameData data;
80
+ data.method = "OTHER THREADS";
81
+ frame_data.push_back(data);
82
+ return 0;
83
+ }
84
+ }
85
+
86
+ for (int i = 0; i < num; i++) {
87
+ VALUE frame = frames_buffer[i];
88
+ frame_data.push_back(cached_frames[frame]);
89
+ }
90
+ return 0;
91
+ }
92
+
93
+ /////
94
+ // For the sake of efficiency this function filters uninteresting frames and
95
+ // does the caching of frames at the same time
96
+ //
97
+ // in-place removal of
98
+ // - frames with line number == 0
99
+ // - all but last of repeated frames
100
+ // - "block" frames (they are confusing) <- revisit
101
+ // and cache uncached frames
102
+ int Frames::remove_garbage(VALUE *frames_buffer, int num) {
103
+ if (num == 1 && (frames_buffer[0] == PR_OTHER_THREAD || frames_buffer[0] == PR_IN_GC))
104
+ return 1;
105
+
106
+ // TODO decide what to do with <cfunc> frames in Ruby 3
107
+
108
+ // 1) ignore top frames where the line number is 0
109
+ // does that mean there is no line number???
110
+ bool found = true;
111
+
112
+ while (found && num > 0) {
113
+ if (cached_frames.count(frames_buffer[num - 1]) == 1) {
114
+ found = (cached_frames[frames_buffer[num - 1]].lineno == 0);
115
+ if (found) num--;
116
+ } else {
117
+ VALUE val = rb_profile_frame_first_lineno(frames_buffer[num - 1]);
118
+ found = (!RB_TYPE_P(val, T_FIXNUM) || !NUM2INT(val));
119
+ if (found) {
120
+ lock_guard<mutex> guard(cached_frames_mutex);
121
+ cached_frames[frames_buffer[num - 1]].lineno = 0;
122
+ num--;
123
+ }
124
+ }
125
+ }
126
+
127
+ // 2) remove all repeated frames, keep the last one
128
+ int count = 0;
129
+ int k = 0;
130
+ found = false;
131
+ while (count < num - k) {
132
+ // is this frame repeated ahead?
133
+ // if so we will replace it with the next one in line
134
+ for (int j = count + k + 1; j < num; j++) {
135
+ if (frames_buffer[count] == frames_buffer[j]) {
136
+ found = true;
137
+ break;
138
+ }
139
+ }
140
+
141
+ if (found) {
142
+ // if we found this frame again later in the snapshot
143
+ // we are going to override this one
144
+ // but not if this is going beyond the boundary
145
+ k++;
146
+ if (count + k < num - 1) frames_buffer[count] = frames_buffer[count + k];
147
+ } else {
148
+ count++;
149
+ frames_buffer[count] = frames_buffer[count + k];
150
+ }
151
+ found = false;
152
+ }
153
+
154
+ // 3) remove "block" frames, they are reported inconsistently and mess up
155
+ // the profile in the dashboard
156
+ // 4) while we are at it we also cache all the frames
157
+ // these 2 are combined so we don't have to run this loop twice
158
+ num = count;
159
+ count = 0, k = 0;
160
+ string method;
161
+
162
+ while (count < num - k) {
163
+ frames_buffer[count] = frames_buffer[count + k];
164
+ cache_frame(frames_buffer[count]);
165
+ method = cached_frames[frames_buffer[count]].method;
166
+
167
+ // TODO revisit need to remove block frames, they only appear when the Ruby
168
+ // ____ script is not started with a method and has blocks outside of the
169
+ // ____ methods called and sometimes inside of rack
170
+ if (method.rfind("block ", 0) == 0) {
171
+ k++;
172
+ } else {
173
+ count++;
174
+ }
175
+ }
176
+ return count;
177
+ }
178
+
179
+ // returns the number of the matching frames
180
+ int Frames::num_matching(VALUE *frames_buffer, int num,
181
+ VALUE *prev_frames_buffer, int prev_num) {
182
+ int i;
183
+ int min = std::min(num, prev_num);
184
+
185
+ for (i = 0; i < min; i++) {
186
+ // we have to start from the "top"
187
+ if (frames_buffer[num - 1 - i] != prev_frames_buffer[prev_num - 1 - i]) {
188
+ return i;
189
+ }
190
+ }
191
+ return i;
192
+ }
193
+
194
+ /////////////////////// DEBUGGING HELPER FUNCTIONS /////////////////////////////
195
+ // helper function to print frame from ruby pointers to frame
196
+ void Frames::print_raw_frame_info(VALUE frame) {
197
+ if (frame == PR_IN_GC || frame == PR_OTHER_THREAD) {
198
+ return;
199
+ }
200
+
201
+ VALUE val;
202
+ int lineno;
203
+ string file, klass, method;
204
+
205
+ val = rb_profile_frame_first_lineno(frame); // returns line number
206
+ if (RB_TYPE_P(val, T_FIXNUM)) lineno = NUM2INT(val);
207
+
208
+ val = rb_profile_frame_classpath(frame); // returns class or nil
209
+ if (RB_TYPE_P(val, T_STRING)) klass = RSTRING_PTR(val);
210
+
211
+ val = rb_profile_frame_absolute_path(frame); // returns file, use rb_profile_frame_path() if nil
212
+ if (!RB_TYPE_P(val, T_STRING)) val = rb_profile_frame_path(frame);
213
+ if (RB_TYPE_P(val, T_STRING)) file = RSTRING_PTR(val);
214
+
215
+ val = rb_profile_frame_label(frame); // returns method or block
216
+ if (RB_TYPE_P(val, T_STRING)) method = RSTRING_PTR(val);
217
+
218
+ cout << " " << frame << " "
219
+ << "L: " << lineno << " "
220
+ << "F: " << file << " "
221
+ << "C: " << klass << " "
222
+ << "M: " << method << endl;
223
+ }
224
+
225
+ void Frames::print_all_raw_frames(VALUE *frames_buffer, int num) {
226
+ for (int i = 0; i < num; i++) {
227
+ print_raw_frame_info(frames_buffer[i]);
228
+ }
229
+ }
230
+
231
+ // helper function to print frame info
232
+ void Frames::print_frame_info(VALUE frame) {
233
+ if (cached_frames.find(frame) != cached_frames.end())
234
+ std::cout << cached_frames[frame].lineno << " "
235
+ << cached_frames[frame].file << " "
236
+ << cached_frames[frame].klass << " "
237
+ << cached_frames[frame].method << std::endl;
238
+ }
239
+
240
+ // helper function for printing the cached frames
241
+ void Frames::print_cached_frames() {
242
+ std::cout << "cached_frames contains:" << endl;
243
+ for (auto it = cached_frames.cbegin(); it != cached_frames.cend(); ++it)
244
+ std::cout << " " << it->first << " - " << it->second.method << ":" << it->second.lineno << endl; // cannot modify *it
245
+ std::cout << std::endl;
246
+ }