Skip to content

Commit fa6d6c5

Browse files
committed
feat(playground): adding playground draft modules, for experimentations more than publishing it
1 parent 57dbf8f commit fa6d6c5

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

draft/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
add_subdirectory(database)
22
add_subdirectory(json)
3+
add_subdirectory(playground)

draft/playground/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
project(playground)
4+
5+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
6+
7+
add_library(${PROJECT_NAME} SHARED main.cpp)
8+
9+
target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE ${ark_SOURCE_DIR}/include)
10+
target_link_libraries(${PROJECT_NAME} PRIVATE ArkReactor)
11+
12+
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
13+
14+
add_custom_command(TARGET ${PROJECT_NAME}
15+
POST_BUILD
16+
COMMAND ${CMAKE_COMMAND} -E copy
17+
"$<TARGET_FILE:${PROJECT_NAME}>" ${PROJECT_SOURCE_DIR}/../../../${PROJECT_NAME}.arkm)

draft/playground/main.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Author: David Robert Nadeau
3+
* Site: http://NadeauSoftware.com/
4+
* License: Creative Commons Attribution 3.0 Unported License
5+
* http://creativecommons.org/licenses/by/3.0/deed.en_US
6+
*/
7+
8+
#if defined(_WIN32)
9+
# include <windows.h>
10+
# include <psapi.h>
11+
12+
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
13+
# include <unistd.h>
14+
# include <sys/resource.h>
15+
16+
# if defined(__APPLE__) && defined(__MACH__)
17+
# include <mach/mach.h>
18+
19+
# elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
20+
# include <fcntl.h>
21+
# include <procfs.h>
22+
23+
# elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
24+
# include <stdio.h>
25+
26+
# endif
27+
28+
#else
29+
# error "Cannot define getPeakRSS( ) or getCurrentRSS( ) for an unknown OS."
30+
#endif
31+
32+
33+
/**
34+
* Returns the peak (maximum so far) resident set size (physical
35+
* memory use) measured in bytes, or zero if the value cannot be
36+
* determined on this OS.
37+
*/
38+
size_t getPeakRSS()
39+
{
40+
#if defined(_WIN32)
41+
/* Windows -------------------------------------------------- */
42+
PROCESS_MEMORY_COUNTERS info;
43+
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
44+
return (size_t)info.PeakWorkingSetSize;
45+
46+
#elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
47+
/* AIX and Solaris ------------------------------------------ */
48+
struct psinfo psinfo;
49+
int fd = -1;
50+
if ((fd = open("/proc/self/psinfo", O_RDONLY)) == -1)
51+
return (size_t)0L; /* Can't open? */
52+
if (read(fd, &psinfo, sizeof(psinfo)) != sizeof(psinfo))
53+
{
54+
close(fd);
55+
return (size_t)0L; /* Can't read? */
56+
}
57+
close(fd);
58+
return (size_t)(psinfo.pr_rssize * 1024L);
59+
60+
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
61+
/* BSD, Linux, and OSX -------------------------------------- */
62+
struct rusage rusage;
63+
getrusage(RUSAGE_SELF, &rusage);
64+
# if defined(__APPLE__) && defined(__MACH__)
65+
return (size_t)rusage.ru_maxrss;
66+
# else
67+
return (size_t)(rusage.ru_maxrss * 1024L);
68+
# endif
69+
70+
#else
71+
/* Unknown OS ----------------------------------------------- */
72+
return (size_t)0L; /* Unsupported. */
73+
#endif
74+
}
75+
76+
77+
/**
78+
* Returns the current resident set size (physical memory use) measured
79+
* in bytes, or zero if the value cannot be determined on this OS.
80+
*/
81+
size_t getCurrentRSS()
82+
{
83+
#if defined(_WIN32)
84+
/* Windows -------------------------------------------------- */
85+
PROCESS_MEMORY_COUNTERS info;
86+
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
87+
return (size_t)info.WorkingSetSize;
88+
89+
#elif defined(__APPLE__) && defined(__MACH__)
90+
/* OSX ------------------------------------------------------ */
91+
struct mach_task_basic_info info;
92+
mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
93+
if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO,
94+
(task_info_t)&info, &infoCount) != KERN_SUCCESS)
95+
return (size_t)0L; /* Can't access? */
96+
return (size_t)info.resident_size;
97+
98+
#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
99+
/* Linux ---------------------------------------------------- */
100+
long rss = 0L;
101+
FILE* fp = NULL;
102+
if ((fp = fopen("/proc/self/statm", "r")) == NULL)
103+
return (size_t)0L; /* Can't open? */
104+
if (fscanf(fp, "%*s%ld", &rss) != 1)
105+
{
106+
fclose(fp);
107+
return (size_t)0L; /* Can't read? */
108+
}
109+
fclose(fp);
110+
return (size_t)rss * (size_t)sysconf(_SC_PAGESIZE);
111+
112+
#else
113+
/* AIX, BSD, Solaris, and Unknown OS ------------------------ */
114+
return (size_t)0L; /* Unsupported. */
115+
#endif
116+
}
117+
118+
#include <Ark/Module.hpp>
119+
120+
// cppcheck-suppress [constParameterReference, constParameterPointer]
121+
Ark::Value ArkGetCurrentRSS(std::vector<Ark::Value>& n [[maybe_unused]], Ark::VM* vm [[maybe_unused]])
122+
{
123+
return Ark::Value(static_cast<double>(getCurrentRSS()));
124+
}
125+
126+
// cppcheck-suppress [constParameterReference, constParameterPointer]
127+
Ark::Value ArkGetPeakRSS(std::vector<Ark::Value>& n, Ark::VM* vm [[maybe_unused]])
128+
{
129+
return Ark::Value(static_cast<double>(getPeakRSS()));
130+
}
131+
132+
ARK_API Ark::mapping* getFunctionsMapping()
133+
{
134+
static Ark::mapping map[] = {
135+
{ "playground:getCurrentRss", ArkGetCurrentRSS},
136+
{ "playground:getPeakRss", ArkGetPeakRSS},
137+
{ nullptr, nullptr }
138+
};
139+
140+
return map;
141+
}

0 commit comments

Comments
 (0)