Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit 90ba653

Browse files
committed
Add a functionality to adjust ext memorry on a per-object level
1 parent 5102961 commit 90ba653

24 files changed

+501
-29
lines changed

config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ if test "$PHP_V8" != "no"; then
187187
v8.cc \
188188
src/php_v8_a.cc \
189189
src/php_v8_exception.cc \
190+
src/php_v8_ext_mem_interface.cc \
190191
src/php_v8_try_catch.cc \
191192
src/php_v8_message.cc \
192193
src/php_v8_stack_frame.cc \

src/php_v8_callbacks.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "php_v8_value.h"
3333
#include "php_v8_isolate.h"
3434
#include <string>
35+
#include <algorithm>
3536

3637
namespace phpv8 {
3738

@@ -169,8 +170,7 @@ namespace phpv8 {
169170
}
170171

171172
int64_t PersistentData::adjustSize(int64_t change_in_bytes) {
172-
adjusted_size_ += change_in_bytes;
173-
assert(adjusted_size_ >= 0);
173+
adjusted_size_ = std::max(static_cast<int64_t>(0), adjusted_size_ + change_in_bytes);
174174
return adjusted_size_;
175175
}
176176
}

src/php_v8_callbacks.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace phpv8 {
2424

2525

2626
#include <v8.h>
27+
#include <limits>
2728
#include <map>
2829
#include <string>
2930
#include <utility>
@@ -128,17 +129,29 @@ namespace phpv8 {
128129
return buckets.empty();
129130
}
130131

131-
int64_t calculateSize();
132-
133132
inline int64_t getTotalSize() {
134133
if (!size_) {
135-
size_ = calculateSize();
134+
// TODO: if adjusted_size_ is going to be much larger than estimated calculateSize() value,
135+
// we can ignore calculateSize() without loosing idea to notify v8 about
136+
// significant external memory pressure
137+
size_ = calculateSize() + adjusted_size_;
138+
139+
if (size_ < 0) {
140+
size_ = std::numeric_limits<int64_t>::max();
141+
}
136142
}
137143

138-
return size_ + adjusted_size_;
144+
return size_;
145+
}
146+
147+
inline int64_t getAdjustedSize() {
148+
return adjusted_size_;
139149
}
140150

141151
int64_t adjustSize(int64_t change_in_bytes);
152+
153+
protected:
154+
int64_t calculateSize();
142155
private:
143156
int64_t size_;
144157
int64_t adjusted_size_;

src/php_v8_ext_mem_interface.cc

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| This file is part of the pinepain/php-v8 PHP extension. |
4+
| |
5+
| Copyright (c) 2015-2016 Bogdan Padalko <pinepain@gmail.com> |
6+
| |
7+
| Licensed under the MIT license: http://opensource.org/licenses/MIT |
8+
| |
9+
| For the full copyright and license information, please view the |
10+
| LICENSE file that was distributed with this source or visit |
11+
| http://opensource.org/licenses/MIT |
12+
+----------------------------------------------------------------------+
13+
*/
14+
15+
#ifdef HAVE_CONFIG_H
16+
#include "config.h"
17+
#endif
18+
19+
#include "php_v8_ext_mem_interface.h"
20+
#include "php_v8_object_template.h"
21+
#include "php_v8_function_template.h"
22+
#include "php_v8_value.h"
23+
#include "php_v8.h"
24+
25+
zend_class_entry *php_v8_ext_mem_interface_ce;
26+
#define this_ce php_v8_ext_mem_interface_ce
27+
28+
void php_v8_ext_mem_interface_value_AdjustExternalAllocatedMemory(INTERNAL_FUNCTION_PARAMETERS) {
29+
zend_long change_in_bytes;
30+
31+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &change_in_bytes) == FAILURE) {
32+
return;
33+
}
34+
35+
PHP_V8_VALUE_FETCH_INTO(getThis(), php_v8_value);
36+
37+
RETURN_LONG(php_v8_value->persistent_data->adjustSize(change_in_bytes));
38+
}
39+
40+
void php_v8_ext_mem_interface_value_GetExternalAllocatedMemory(INTERNAL_FUNCTION_PARAMETERS) {
41+
if (zend_parse_parameters_none() == FAILURE) {
42+
return;
43+
}
44+
45+
PHP_V8_VALUE_FETCH_INTO(getThis(), php_v8_value);
46+
47+
RETURN_LONG(php_v8_value->persistent_data->getAdjustedSize());
48+
}
49+
50+
51+
void php_v8_ext_mem_interface_function_template_AdjustExternalAllocatedMemory(INTERNAL_FUNCTION_PARAMETERS) {
52+
zend_long change_in_bytes;
53+
54+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &change_in_bytes) == FAILURE) {
55+
return;
56+
}
57+
58+
PHP_V8_OBJECT_TEMPLATE_FETCH_INTO(getThis(), php_v8_object_template);
59+
60+
RETURN_LONG(php_v8_object_template->persistent_data->adjustSize(change_in_bytes));
61+
}
62+
63+
void php_v8_ext_mem_interface_function_template_GetExternalAllocatedMemory(INTERNAL_FUNCTION_PARAMETERS) {
64+
if (zend_parse_parameters_none() == FAILURE) {
65+
return;
66+
}
67+
68+
PHP_V8_OBJECT_TEMPLATE_FETCH_INTO(getThis(), php_v8_object_template);
69+
70+
RETURN_LONG(php_v8_object_template->persistent_data->getAdjustedSize());
71+
}
72+
73+
74+
void php_v8_ext_mem_interface_object_template_AdjustExternalAllocatedMemory(INTERNAL_FUNCTION_PARAMETERS) {
75+
zend_long change_in_bytes;
76+
77+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &change_in_bytes) == FAILURE) {
78+
return;
79+
}
80+
81+
PHP_V8_FUNCTION_TEMPLATE_FETCH_INTO(getThis(), php_v8_function_template);
82+
83+
RETURN_LONG(php_v8_function_template->persistent_data->adjustSize(change_in_bytes));
84+
}
85+
86+
void php_v8_ext_mem_interface_object_template_GetExternalAllocatedMemory(INTERNAL_FUNCTION_PARAMETERS) {
87+
if (zend_parse_parameters_none() == FAILURE) {
88+
return;
89+
}
90+
91+
PHP_V8_FUNCTION_TEMPLATE_FETCH_INTO(getThis(), php_v8_function_template);
92+
93+
RETURN_LONG(php_v8_function_template->persistent_data->getAdjustedSize());
94+
}
95+
96+
97+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_php_v8_ext_mem_interface_AdjustExternalAllocatedMemory, ZEND_RETURN_VALUE, 1, IS_LONG, NULL, 0)
98+
ZEND_ARG_TYPE_INFO(0, change_in_bytes, IS_LONG, 0)
99+
ZEND_END_ARG_INFO()
100+
101+
102+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_php_v8_ext_mem_interface_GetExternalAllocatedMemory, ZEND_RETURN_VALUE, 0, IS_LONG, NULL, 0)
103+
ZEND_END_ARG_INFO()
104+
105+
106+
static const zend_function_entry php_v8_ext_mem_interface_methods[] = {
107+
PHP_ABSTRACT_ME(V8AdjustableExternalMemoryInterface, AdjustExternalAllocatedMemory, arginfo_php_v8_ext_mem_interface_AdjustExternalAllocatedMemory)
108+
PHP_ABSTRACT_ME(V8AdjustableExternalMemoryInterface, GetExternalAllocatedMemory, arginfo_php_v8_ext_mem_interface_GetExternalAllocatedMemory)
109+
110+
PHP_FE_END
111+
};
112+
113+
PHP_MINIT_FUNCTION (php_v8_ext_mem_interface) {
114+
zend_class_entry ce;
115+
116+
INIT_NS_CLASS_ENTRY(ce, PHP_V8_NS, "AdjustableExternalMemoryInterface", php_v8_ext_mem_interface_methods);
117+
this_ce = zend_register_internal_interface(&ce);
118+
119+
return SUCCESS;
120+
}
121+
122+
123+
/*
124+
* Local variables:
125+
* tab-width: 4
126+
* c-basic-offset: 4
127+
* End:
128+
* vim600: noet sw=4 ts=4 fdm=marker
129+
* vim<600: noet sw=4 ts=4
130+
*/

src/php_v8_ext_mem_interface.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| This file is part of the pinepain/php-v8 PHP extension. |
4+
| |
5+
| Copyright (c) 2015-2016 Bogdan Padalko <pinepain@gmail.com> |
6+
| |
7+
| Licensed under the MIT license: http://opensource.org/licenses/MIT |
8+
| |
9+
| For the full copyright and license information, please view the |
10+
| LICENSE file that was distributed with this source or visit |
11+
| http://opensource.org/licenses/MIT |
12+
+----------------------------------------------------------------------+
13+
*/
14+
15+
#ifndef PHP_V8_EXT_MEM_INTERFACE_H
16+
#define PHP_V8_EXT_MEM_INTERFACE_H
17+
18+
19+
extern "C" {
20+
#include "php.h"
21+
22+
#ifdef ZTS
23+
#include "TSRM.h"
24+
#endif
25+
}
26+
27+
28+
extern zend_class_entry* php_v8_ext_mem_interface_ce;
29+
30+
31+
extern void php_v8_ext_mem_interface_value_AdjustExternalAllocatedMemory(INTERNAL_FUNCTION_PARAMETERS);
32+
extern void php_v8_ext_mem_interface_value_GetExternalAllocatedMemory(INTERNAL_FUNCTION_PARAMETERS);
33+
34+
extern void php_v8_ext_mem_interface_function_template_AdjustExternalAllocatedMemory(INTERNAL_FUNCTION_PARAMETERS);
35+
extern void php_v8_ext_mem_interface_function_template_GetExternalAllocatedMemory(INTERNAL_FUNCTION_PARAMETERS);
36+
37+
extern void php_v8_ext_mem_interface_object_template_AdjustExternalAllocatedMemory(INTERNAL_FUNCTION_PARAMETERS);
38+
extern void php_v8_ext_mem_interface_object_template_GetExternalAllocatedMemory(INTERNAL_FUNCTION_PARAMETERS);
39+
40+
PHP_MINIT_FUNCTION(php_v8_ext_mem_interface);
41+
42+
#endif //PHP_V8_EXT_MEM_INTERFACE_H
43+
44+
/*
45+
* Local variables:
46+
* tab-width: 4
47+
* c-basic-offset: 4
48+
* End:
49+
* vim600: noet sw=4 ts=4 fdm=marker
50+
* vim<600: noet sw=4 ts=4
51+
*/

src/php_v8_function_template.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "php_v8_object.h"
2424
#include "php_v8_value.h"
2525
#include "php_v8_context.h"
26+
#include "php_v8_ext_mem_interface.h"
2627
#include "php_v8.h"
2728

2829
zend_class_entry *php_v8_function_template_class_entry;
@@ -437,6 +438,16 @@ static PHP_METHOD(V8FunctionTemplate, HasInstance) {
437438
RETURN_BOOL(local_template->HasInstance(local_obj));
438439
}
439440

441+
/* Non-standard, implementations of AdjustableExternalMemoryInterface::AdjustExternalAllocatedMemory */
442+
static PHP_METHOD(V8FunctionTemplate, AdjustExternalAllocatedMemory) {
443+
php_v8_ext_mem_interface_function_template_AdjustExternalAllocatedMemory(INTERNAL_FUNCTION_PARAM_PASSTHRU);
444+
}
445+
446+
/* Non-standard, implementations of AdjustableExternalMemoryInterface::GetExternalAllocatedMemory */
447+
static PHP_METHOD(V8FunctionTemplate, GetExternalAllocatedMemory) {
448+
php_v8_ext_mem_interface_function_template_GetExternalAllocatedMemory(INTERNAL_FUNCTION_PARAM_PASSTHRU);
449+
}
450+
440451

441452
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_function_template___construct, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
442453
ZEND_ARG_OBJ_INFO(0, isolate, V8\\Isolate, 0)
@@ -525,6 +536,14 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_function_template_HasInstance
525536
ZEND_ARG_OBJ_INFO(0, object, V8\\ObjectValue, 0)
526537
ZEND_END_ARG_INFO()
527538

539+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_function_template_AdjustExternalAllocatedMemory, ZEND_RETURN_VALUE, 1, IS_LONG, NULL, 0)
540+
ZEND_ARG_TYPE_INFO(0, change_in_bytes, IS_LONG, 0)
541+
ZEND_END_ARG_INFO()
542+
543+
544+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_function_template_GetExternalAllocatedMemory, ZEND_RETURN_VALUE, 0, IS_LONG, NULL, 0)
545+
ZEND_END_ARG_INFO()
546+
528547

529548
static const zend_function_entry php_v8_function_template_methods[] = {
530549
PHP_ME(V8FunctionTemplate, __construct, arginfo_v8_function_template___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
@@ -547,6 +566,9 @@ static const zend_function_entry php_v8_function_template_methods[] = {
547566
PHP_ME(V8FunctionTemplate, RemovePrototype, arginfo_v8_function_template_RemovePrototype, ZEND_ACC_PUBLIC)
548567
PHP_ME(V8FunctionTemplate, HasInstance, arginfo_v8_function_template_HasInstance, ZEND_ACC_PUBLIC)
549568

569+
PHP_ME(V8FunctionTemplate, AdjustExternalAllocatedMemory, arginfo_v8_function_template_AdjustExternalAllocatedMemory, ZEND_ACC_PUBLIC)
570+
PHP_ME(V8FunctionTemplate, GetExternalAllocatedMemory, arginfo_v8_function_template_GetExternalAllocatedMemory, ZEND_ACC_PUBLIC)
571+
550572
PHP_FE_END
551573
};
552574

@@ -556,6 +578,7 @@ PHP_MINIT_FUNCTION (php_v8_function_template) {
556578

557579
INIT_NS_CLASS_ENTRY(ce, PHP_V8_NS, "FunctionTemplate", php_v8_function_template_methods);
558580
this_ce = zend_register_internal_class_ex(&ce, php_v8_template_ce);
581+
zend_class_implements(this_ce, 1, php_v8_ext_mem_interface_ce);
559582
this_ce->create_object = php_v8_function_template_ctor;
560583

561584
memcpy(&php_v8_function_template_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));

src/php_v8_object.cc

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "php_v8_name.h"
2929
#include "php_v8_value.h"
3030
#include "php_v8_context.h"
31+
#include "php_v8_ext_mem_interface.h"
3132
#include "php_v8.h"
3233

3334

@@ -1437,6 +1438,17 @@ static PHP_METHOD(V8Object, CallAsConstructor) {
14371438
//static PHP_METHOD(V8Object, Cast) {
14381439
//}
14391440

1441+
/* Non-standard, implementations of AdjustableExternalMemoryInterface::AdjustExternalAllocatedMemory */
1442+
static PHP_METHOD(V8Object, AdjustExternalAllocatedMemory) {
1443+
php_v8_ext_mem_interface_value_AdjustExternalAllocatedMemory(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1444+
}
1445+
1446+
/* Non-standard, implementations of AdjustableExternalMemoryInterface::GetExternalAllocatedMemory */
1447+
static PHP_METHOD(V8Object, GetExternalAllocatedMemory) {
1448+
php_v8_ext_mem_interface_value_GetExternalAllocatedMemory(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1449+
}
1450+
1451+
14401452
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_object___construct, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
14411453
ZEND_ARG_OBJ_INFO(0, context, V8\\Context, 0)
14421454
ZEND_END_ARG_INFO()
@@ -1645,6 +1657,14 @@ ZEND_END_ARG_INFO()
16451657
// ZEND_ARG_OBJ_INFO(0, persistent, V8\\Value, 0)
16461658
//ZEND_END_ARG_INFO()
16471659

1660+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_object_AdjustExternalAllocatedMemory, ZEND_RETURN_VALUE, 1, IS_LONG, NULL, 0)
1661+
ZEND_ARG_TYPE_INFO(0, change_in_bytes, IS_LONG, 0)
1662+
ZEND_END_ARG_INFO()
1663+
1664+
1665+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_object_GetExternalAllocatedMemory, ZEND_RETURN_VALUE, 0, IS_LONG, NULL, 0)
1666+
ZEND_END_ARG_INFO()
1667+
16481668

16491669
static const zend_function_entry php_v8_object_methods[] = {
16501670
PHP_ME(V8Object, __construct, arginfo_v8_object___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
@@ -1696,15 +1716,19 @@ static const zend_function_entry php_v8_object_methods[] = {
16961716

16971717
// NOTE: Not supported yet
16981718
//PHP_ME(V8Object, Cast, arginfo_v8_object_Cast, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
1719+
1720+
PHP_ME(V8Object, AdjustExternalAllocatedMemory, arginfo_v8_object_AdjustExternalAllocatedMemory, ZEND_ACC_PUBLIC)
1721+
PHP_ME(V8Object, GetExternalAllocatedMemory, arginfo_v8_object_GetExternalAllocatedMemory, ZEND_ACC_PUBLIC)
1722+
16991723
PHP_FE_END
17001724
};
17011725

17021726

1703-
17041727
PHP_MINIT_FUNCTION(php_v8_object) {
17051728
zend_class_entry ce;
17061729
INIT_NS_CLASS_ENTRY(ce, PHP_V8_NS, "ObjectValue", php_v8_object_methods);
17071730
this_ce = zend_register_internal_class_ex(&ce, php_v8_value_class_entry);
1731+
zend_class_implements(this_ce, 1, php_v8_ext_mem_interface_ce);
17081732

17091733
zend_declare_property_null(this_ce, ZEND_STRL("context"), ZEND_ACC_PRIVATE);
17101734

0 commit comments

Comments
 (0)