Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 79 additions & 135 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@types/tmp": "0.1.0",
"codecov": "^3.0.0",
"deep-copy": "^1.4.2",
"gts": "^0.9.0",
"gts": "^1.0.0",
"js-green-licenses": "^1.0.0",
"linkinator": "^1.4.2",
"mocha": "^6.1.4",
Expand Down
15 changes: 10 additions & 5 deletions ts/src/heap-profiler-bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@

import * as path from 'path';

import {AllocationProfileNode} from './v8-types';
import { AllocationProfileNode } from './v8-types';

const binary = require('node-pre-gyp');
const bindingPath =
binary.find(path.resolve(path.join(__dirname, '../../package.json')));
const bindingPath = binary.find(
path.resolve(path.join(__dirname, '../../package.json'))
);
const profiler = require(bindingPath);

// Wrappers around native heap profiler functions.

export function startSamplingHeapProfiler(
heapIntervalBytes: number, heapStackDepth: number) {
heapIntervalBytes: number,
heapStackDepth: number
) {
profiler.heapProfiler.startSamplingHeapProfiler(
heapIntervalBytes, heapStackDepth);
heapIntervalBytes,
heapStackDepth
);
}

export function stopSamplingHeapProfiler() {
Expand Down
37 changes: 24 additions & 13 deletions ts/src/heap-profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
* limitations under the License.
*/

import {perftools} from '../../proto/profile';
import { perftools } from '../../proto/profile';

import {getAllocationProfile, startSamplingHeapProfiler, stopSamplingHeapProfiler} from './heap-profiler-bindings';
import {serializeHeapProfile} from './profile-serializer';
import {SourceMapper} from './sourcemapper/sourcemapper';
import {AllocationProfileNode} from './v8-types';
import {
getAllocationProfile,
startSamplingHeapProfiler,
stopSamplingHeapProfiler,
} from './heap-profiler-bindings';
import { serializeHeapProfile } from './profile-serializer';
import { SourceMapper } from './sourcemapper/sourcemapper';
import { AllocationProfileNode } from './v8-types';

let enabled = false;
let heapIntervalBytes = 0;
Expand All @@ -45,27 +49,33 @@ export function v8Profile(): AllocationProfileNode {
* @param ignoreSamplePath
* @param sourceMapper
*/
export function profile(ignoreSamplePath?: string, sourceMapper?: SourceMapper):
perftools.profiles.IProfile {
export function profile(
ignoreSamplePath?: string,
sourceMapper?: SourceMapper
): perftools.profiles.IProfile {
const startTimeNanos = Date.now() * 1000 * 1000;
const result = v8Profile();
// Add node for external memory usage.
// Current type definitions do not have external.
// TODO: remove any once type definition is updated to include external.
// tslint:disable-next-line: no-any
const {external}: {external: number} = process.memoryUsage() as any;
const { external }: { external: number } = process.memoryUsage() as any;
if (external > 0) {
const externalNode: AllocationProfileNode = {
name: '(external)',
scriptName: '',
children: [],
allocations: [{sizeBytes: external, count: 1}],
allocations: [{ sizeBytes: external, count: 1 }],
};
result.children.push(externalNode);
}
return serializeHeapProfile(
result, startTimeNanos, heapIntervalBytes, ignoreSamplePath,
sourceMapper);
result,
startTimeNanos,
heapIntervalBytes,
ignoreSamplePath,
sourceMapper
);
}

/**
Expand All @@ -78,8 +88,9 @@ export function profile(ignoreSamplePath?: string, sourceMapper?: SourceMapper):
*/
export function start(intervalBytes: number, stackDepth: number) {
if (enabled) {
throw new Error(`Heap profiler is already started with intervalBytes ${
heapIntervalBytes} and stackDepth ${stackDepth}`);
throw new Error(
`Heap profiler is already started with intervalBytes ${heapIntervalBytes} and stackDepth ${stackDepth}`
);
}
heapIntervalBytes = intervalBytes;
heapStackDepth = stackDepth;
Expand Down
12 changes: 6 additions & 6 deletions ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {writeFileSync} from 'fs';
import {gzipSync} from 'zlib';
import { writeFileSync } from 'fs';
import { gzipSync } from 'zlib';

import {perftools} from '../../proto/profile';
import { perftools } from '../../proto/profile';

import * as heapProfiler from './heap-profiler';
import {encodeSync} from './profile-encoder';
import { encodeSync } from './profile-encoder';
import * as timeProfiler from './time-profiler';

export {encode, encodeSync} from './profile-encoder';
export {SourceMapper} from './sourcemapper/sourcemapper';
export { encode, encodeSync } from './profile-encoder';
export { SourceMapper } from './sourcemapper/sourcemapper';

export const time = {
profile: timeProfiler.profile,
Expand Down
9 changes: 5 additions & 4 deletions ts/src/profile-encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/

import * as pify from 'pify';
import {gzip, gzipSync} from 'zlib';
import { gzip, gzipSync } from 'zlib';

import {perftools} from '../../proto/profile';
import { perftools } from '../../proto/profile';

const gzipPromise = pify(gzip);

export async function encode(profile: perftools.profiles.IProfile):
Promise<Buffer> {
export async function encode(
profile: perftools.profiles.IProfile
): Promise<Buffer> {
const buffer = perftools.profiles.Profile.encode(profile).finish();
return gzipPromise(buffer);
}
Expand Down
Loading