gc-trigger 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -0,0 +1,5 @@
1
+ # News
2
+
3
+ ## 1.0.0: 2016-06-01
4
+
5
+ The first release!!!
@@ -0,0 +1,18 @@
1
+ # Copyright (C) 2016 Kouhei Sutou <kou@clear-code.com>
2
+ #
3
+ # This library is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU Lesser General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # This library is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public License
14
+ # along with this library. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ require "mkmf"
17
+
18
+ create_makefile("gc_trigger")
@@ -0,0 +1,82 @@
1
+ /*
2
+ * Copyright (C) 2016 Kouhei Sutou <kou@clear-code.com>
3
+ *
4
+ * This library is free software: you can redistribute it and/or modify
5
+ * it under the terms of the GNU Lesser General Public License as published by
6
+ * the Free Software Foundation, either version 3 of the License, or
7
+ * (at your option) any later version.
8
+ *
9
+ * This library is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ * GNU Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public License
15
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ #include <ruby.h>
19
+ #include <ruby/thread_native.h>
20
+
21
+ #define RB_GC_TRIGGER_DLL
22
+ #include "rb-gc-trigger.h"
23
+
24
+ static rb_nativethread_lock_t memory_usage_lock;
25
+ static int64_t memory_usage_current = 0;
26
+ static int64_t memory_usage_threshold = 4096;
27
+
28
+ void
29
+ rb_gc_trigger_update_memory_usage(int64_t diff)
30
+ {
31
+ int need_gc;
32
+
33
+ memory_usage_current += diff;
34
+
35
+ if (diff <= 0) {
36
+ return;
37
+ }
38
+
39
+ if (rb_during_gc()) {
40
+ return;
41
+ }
42
+
43
+ rb_nativethread_lock_lock(&memory_usage_lock);
44
+ need_gc = (memory_usage_current > memory_usage_threshold);
45
+ rb_nativethread_lock_unlock(&memory_usage_lock);
46
+ if (!need_gc) {
47
+ return;
48
+ }
49
+
50
+ rb_gc();
51
+ rb_nativethread_lock_lock(&memory_usage_lock);
52
+ if (memory_usage_current > memory_usage_threshold) {
53
+ memory_usage_threshold *= 2;
54
+ }
55
+ rb_nativethread_lock_unlock(&memory_usage_lock);
56
+ }
57
+
58
+ static VALUE
59
+ update_memory_usage(VALUE module, VALUE rb_diff)
60
+ {
61
+ int64_t diff;
62
+
63
+ diff = NUM2LL(rb_diff);
64
+ rb_gc_trigger_update_memory_usage(diff);
65
+
66
+ return Qnil;
67
+ }
68
+
69
+ void
70
+ Init_gc_trigger(void)
71
+ {
72
+ VALUE gc_trigger;
73
+
74
+ rb_nativethread_lock_initialize(&memory_usage_lock);
75
+
76
+ gc_trigger = rb_define_module("GCTrigger");
77
+
78
+ rb_define_singleton_method(gc_trigger,
79
+ "update_memory_usage",
80
+ update_memory_usage,
81
+ 1);
82
+ }
@@ -0,0 +1,41 @@
1
+ /*
2
+ * Copyright (C) 2016 Kouhei Sutou <kou@clear-code.com>
3
+ *
4
+ * This library is free software: you can redistribute it and/or modify
5
+ * it under the terms of the GNU Lesser General Public License as published by
6
+ * the Free Software Foundation, either version 3 of the License, or
7
+ * (at your option) any later version.
8
+ *
9
+ * This library is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ * GNU Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public License
15
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ #pragma once
19
+
20
+ #include <stdint.h>
21
+
22
+ #if defined(__cplusplus)
23
+ # define RB_GC_TRIGGER_BEGIN_DECLS extern "C" {
24
+ # define RB_GC_TRIGGER_END_DECLS }
25
+ #else
26
+ # define RB_GC_TRIGGER_BEGIN_DECLS
27
+ # define RB_GC_TRIGGER_END_DECLS
28
+ #endif
29
+
30
+ #ifdef _WIN32
31
+ # define RB_GC_TRIGGER_API __declspec(dllexport)
32
+ #else
33
+ # define RB_GC_TRIGGER_API
34
+ #endif
35
+
36
+ RB_GC_TRIGGER_BEGIN_DECLS
37
+
38
+ RB_GC_TRIGGER_API void rb_gc_trigger_update_memory_usage(int64_t diff);
39
+ RB_GC_TRIGGER_API void Init_gc_trigger(void);
40
+
41
+ RB_GC_TRIGGER_END_DECLS
@@ -0,0 +1,47 @@
1
+ # -*- ruby -*-
2
+ #
3
+ # Copyright (C) 2016 Kouhei Sutou <kou@clear-code.com>
4
+ #
5
+ # This library is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU Lesser General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public License
16
+ # along with this library. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ clean_white_space = lambda do |entry|
19
+ entry.gsub(/(\A\n+|\n+\z)/, '') + "\n"
20
+ end
21
+
22
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "lib"))
23
+ require "gc-trigger/version"
24
+
25
+ Gem::Specification.new do |spec|
26
+ spec.name = "gc-trigger"
27
+ spec.version = GCTrigger::VERSION
28
+ spec.homepage = "https://github.com/kou/gc-trigger"
29
+ spec.authors = ["Kouhei Sutou"]
30
+ spec.email = ["kou@clear-code.com"]
31
+ readme = File.read("README.md", :encoding => "UTF-8")
32
+ entries = readme.split(/^\#\#\s(.*)$/)
33
+ description = clean_white_space.call(entries[entries.index("Description") + 1])
34
+ spec.summary, spec.description, = description.split(/\n\n+/, 3)
35
+ spec.license = "LGPL-3.0+"
36
+ spec.files = ["README.md", "Rakefile", "Gemfile", "#{spec.name}.gemspec"]
37
+ spec.files += Dir.glob("lib/**/*.rb")
38
+ spec.files += Dir.glob("ext/**/*.{c,h,rb}")
39
+ spec.files += Dir.glob("doc/text/*")
40
+ spec.files += Dir.glob("sample/**/*.rb")
41
+ spec.extensions += ["ext/gc_trigger/extconf.rb"]
42
+
43
+ spec.add_development_dependency("bundler")
44
+ spec.add_development_dependency("rake")
45
+ spec.add_development_dependency("rake-compiler")
46
+ spec.add_development_dependency("rake-compiler-dock")
47
+ end
@@ -0,0 +1,23 @@
1
+ # Copyright (C) 2016 Kouhei Sutou <kou@clear-code.com>
2
+ #
3
+ # This library is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU Lesser General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # This library is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public License
14
+ # along with this library. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ begin
17
+ major, minor, _ = RUBY_VERSION.split(".")
18
+ require "#{major}.#{minor}/gc_trigger.so"
19
+ rescue LoadError
20
+ require "gc_trigger.so"
21
+ end
22
+
23
+ require "gc-trigger/version"
@@ -0,0 +1,18 @@
1
+ # Copyright (C) 2016 Kouhei Sutou <kou@clear-code.com>
2
+ #
3
+ # This library is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU Lesser General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # This library is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public License
14
+ # along with this library. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ module GCTrigger
17
+ VERSION = "1.0.0"
18
+ end
@@ -0,0 +1,25 @@
1
+ require "gc-trigger"
2
+ require "cairo"
3
+
4
+ def image_surface_finalizer(size)
5
+ lambda do |id|
6
+ GCTrigger.update_memory_usage(-size)
7
+ end
8
+ end
9
+
10
+ def create_image_surface(width, height)
11
+ size = 4 * width * height
12
+ surface = Cairo::ImageSurface.new(:argb32, width, height)
13
+ GCTrigger.update_memory_usage(size)
14
+ ObjectSpace.define_finalizer(surface, image_surface_finalizer(size))
15
+ surface
16
+ end
17
+
18
+ 10000.times do |i|
19
+ create_image_surface(6000, 6000)
20
+ case File.readlines("/proc/self/status").grep(/\AVmRSS:/)[0]
21
+ when /\AVmRSS:\s+(\d+)\s+kB/
22
+ vm_rss = $1.to_i * 1024
23
+ end
24
+ puts [GC.count, i, vm_rss].join(",")
25
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gc-trigger
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kouhei Sutou
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake-compiler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake-compiler-dock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: 'Ruby''s GC doesn''t care about memory usage of external libraries. It
70
+ means that Ruby''s GC isn''t run when extension library uses huge memories in external
71
+ libraries. For example, Ruby''s GC isn''t run when you open many images by the extension
72
+ library.
73
+
74
+ '
75
+ email:
76
+ - kou@clear-code.com
77
+ executables: []
78
+ extensions:
79
+ - ext/gc_trigger/extconf.rb
80
+ extra_rdoc_files: []
81
+ files:
82
+ - Gemfile
83
+ - README.md
84
+ - Rakefile
85
+ - doc/text/gpl-3.txt
86
+ - doc/text/lgpl-3.txt
87
+ - doc/text/news.md
88
+ - ext/gc_trigger/extconf.rb
89
+ - ext/gc_trigger/gc-trigger.c
90
+ - ext/gc_trigger/rb-gc-trigger.h
91
+ - gc-trigger.gemspec
92
+ - lib/gc-trigger.rb
93
+ - lib/gc-trigger/version.rb
94
+ - sample/cairo.rb
95
+ homepage: https://github.com/kou/gc-trigger
96
+ licenses:
97
+ - LGPL-3.0+
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.5.1
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: gc-trigger is a library for extension libraries that use many memories in
119
+ external libraries. For example, an extension library that manipulates images is
120
+ a target library.
121
+ test_files: []
122
+ has_rdoc: