Skip to content

Commit f47851d

Browse files
feat: add newer instrument_hooks markers to native_core
1 parent aa9d127 commit f47851d

6 files changed

Lines changed: 82 additions & 1 deletion

File tree

packages/core/rollup.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineConfig } from "rollup";
22
import { declarationsPlugin, jsPlugins } from "../../rollup.options";
33

4-
import pkg from "./package.json" assert { type: "json" };
4+
import pkg from "./package.json" with { type: "json" };
55
const entrypoint = "src/index.ts";
66

77
export default defineConfig([

packages/core/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,9 @@ export * from "./utils";
8181
export * from "./walltime";
8282
export type { InstrumentMode };
8383
export const InstrumentHooks = native_core.InstrumentHooks;
84+
export {
85+
MARKER_TYPE_SAMPLE_START,
86+
MARKER_TYPE_SAMPLE_END,
87+
MARKER_TYPE_BENCHMARK_START,
88+
MARKER_TYPE_BENCHMARK_END,
89+
} from "./native_core/instruments/hooks";

packages/core/src/native_core/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ try {
5959
writeEnvironment: (_pid: number) => {
6060
return 0;
6161
},
62+
currentTimestamp: () => {
63+
return 0n;
64+
},
65+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
66+
addMarker: (_pid: number, _markerType: number, _timestamp: bigint) => {
67+
return 0;
68+
},
6269
__codspeed_root_frame__: <T>(callback: () => T): T => {
6370
return callback();
6471
},

packages/core/src/native_core/instruments/hooks.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,29 @@ export interface InstrumentHooks {
4848
*/
4949
writeEnvironment(pid: number): number;
5050

51+
/**
52+
* Returns a high-resolution timestamp (nanoseconds) suitable for marker
53+
* emission. Mirrors `instrument_hooks_current_timestamp` in the C API.
54+
*/
55+
currentTimestamp(): bigint;
56+
57+
/**
58+
* Emit a marker for the given pid at the given timestamp.
59+
* @param pid Process ID
60+
* @param markerType One of MARKER_TYPE_*
61+
* @param timestamp Timestamp (typically obtained from `currentTimestamp()`)
62+
* @returns 0 on success, non-zero on error
63+
*/
64+
addMarker(pid: number, markerType: number, timestamp: bigint): number;
65+
5166
/**
5267
* Execute a callback function with __codspeed_root_frame__ in its stack trace
5368
* @param callback Function to execute
5469
*/
5570
__codspeed_root_frame__<T>(callback: () => T): T;
5671
}
72+
73+
export const MARKER_TYPE_SAMPLE_START = 0;
74+
export const MARKER_TYPE_SAMPLE_END = 1;
75+
export const MARKER_TYPE_BENCHMARK_START = 2;
76+
export const MARKER_TYPE_BENCHMARK_END = 3;

packages/core/src/native_core/instruments/hooks_wrapper.cc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,48 @@ Napi::Number WriteEnvironment(const Napi::CallbackInfo &info) {
129129
return Napi::Number::New(env, result);
130130
}
131131

132+
Napi::BigInt CurrentTimestamp(const Napi::CallbackInfo &info) {
133+
Napi::Env env = info.Env();
134+
uint64_t ts = instrument_hooks_current_timestamp();
135+
return Napi::BigInt::New(env, ts);
136+
}
137+
138+
Napi::Number AddMarker(const Napi::CallbackInfo &info) {
139+
Napi::Env env = info.Env();
140+
141+
if (info.Length() != 3) {
142+
Napi::TypeError::New(env,
143+
"Expected 3 arguments: pid, markerType, timestamp")
144+
.ThrowAsJavaScriptException();
145+
return Napi::Number::New(env, 1);
146+
}
147+
148+
if (!info[0].IsNumber() || !info[1].IsNumber() ||
149+
!(info[2].IsBigInt() || info[2].IsNumber())) {
150+
Napi::TypeError::New(
151+
env,
152+
"Expected number (pid), number (markerType), bigint|number (timestamp)")
153+
.ThrowAsJavaScriptException();
154+
return Napi::Number::New(env, 1);
155+
}
156+
157+
int32_t pid = info[0].As<Napi::Number>().Int32Value();
158+
uint8_t marker_type =
159+
static_cast<uint8_t>(info[1].As<Napi::Number>().Uint32Value());
160+
uint64_t timestamp;
161+
if (info[2].IsBigInt()) {
162+
bool lossless = false;
163+
timestamp = info[2].As<Napi::BigInt>().Uint64Value(&lossless);
164+
} else {
165+
timestamp =
166+
static_cast<uint64_t>(info[2].As<Napi::Number>().DoubleValue());
167+
}
168+
169+
uint8_t result =
170+
instrument_hooks_add_marker(hooks, pid, marker_type, timestamp);
171+
return Napi::Number::New(env, result);
172+
}
173+
132174
Napi::Value __attribute__ ((noinline)) __codspeed_root_frame__(const Napi::CallbackInfo &info) {
133175
Napi::Env env = info.Env();
134176

@@ -169,6 +211,10 @@ Napi::Object Initialize(Napi::Env env, Napi::Object exports) {
169211
Napi::Function::New(env, SetEnvironment));
170212
instrumentHooksObj.Set(Napi::String::New(env, "writeEnvironment"),
171213
Napi::Function::New(env, WriteEnvironment));
214+
instrumentHooksObj.Set(Napi::String::New(env, "currentTimestamp"),
215+
Napi::Function::New(env, CurrentTimestamp));
216+
instrumentHooksObj.Set(Napi::String::New(env, "addMarker"),
217+
Napi::Function::New(env, AddMarker));
172218
instrumentHooksObj.Set(Napi::String::New(env, "__codspeed_root_frame__"),
173219
Napi::Function::New(env, __codspeed_root_frame__));
174220

packages/core/src/native_core/instruments/hooks_wrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Napi::Number SetExecutedBenchmark(const Napi::CallbackInfo &info);
1919
Napi::Number SetIntegration(const Napi::CallbackInfo &info);
2020
Napi::Number SetEnvironment(const Napi::CallbackInfo &info);
2121
Napi::Number WriteEnvironment(const Napi::CallbackInfo &info);
22+
Napi::BigInt CurrentTimestamp(const Napi::CallbackInfo &info);
23+
Napi::Number AddMarker(const Napi::CallbackInfo &info);
2224
Napi::Object Initialize(Napi::Env env, Napi::Object exports);
2325

2426
} // namespace hooks_wrapper

0 commit comments

Comments
 (0)