Skip to content

Commit

Permalink
Merge pull request #452 from NordicSemiconductor/fix/disk-full-trigger
Browse files Browse the repository at this point in the history
Fix/disk full trigger
  • Loading branch information
kylebonnici authored Apr 5, 2024
2 parents b758e5f + 891c4c6 commit 31a85a0
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 25 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

- Chart frame trotting for slower machines.
- Clearing session files on close in some special cases.
- Behavior of **Disk full trigger** when recording for an indefinite period of
time. It now correctly stops sampling when the disk is full.

## 4.0.0 - 2024-03-13

Expand Down
36 changes: 17 additions & 19 deletions src/actions/deviceActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import { setSpikeFilter as persistSpikeFilter } from '../utils/persistentStore';

let device: null | SerialDevice = null;
let updateRequestInterval: NodeJS.Timeout | undefined;
let releaseFileWriteListener: (() => void) | undefined;

export const setupOptions =
(recordingMode: RecordingMode): AppThunk<RootState, Promise<void>> =>
Expand All @@ -99,6 +100,21 @@ export const setupOptions =
DataManager().initializeTriggerSession(60);
break;
}

releaseFileWriteListener?.();
releaseFileWriteListener = DataManager().onFileWrite?.(() => {
isDiskFull(
getDiskFullTrigger(getState()),
getSessionRootFolder(getState())
).then(isFull => {
if (isFull) {
logger.warn(
'Session stopped. Disk full trigger value reached.'
);
dispatch(samplingStop());
}
});
});
} catch (err) {
logger.error(err);
}
Expand Down Expand Up @@ -145,6 +161,7 @@ export const samplingStop =
dispatch(samplingStoppedAction());
await device.ppkAverageStop();
stopPreventSleep();
releaseFileWriteListener?.();
};

export const updateSpikeFilter = (): AppThunk<RootState> => (_, getState) => {
Expand Down Expand Up @@ -207,7 +224,6 @@ export const open =
let prevBits = 0;
let nbSamples = 0;
let nbSamplesTotal = 0;
let lastDiskFullCheck = 0;

const onSample = ({ value, bits }: SampleValues) => {
const {
Expand Down Expand Up @@ -311,24 +327,6 @@ export const open =
if (samplingRunning) {
dispatch(samplingStop());
}

const shouldCheckDiskFull =
performance.now() - lastDiskFullCheck > 10_000;

if (shouldCheckDiskFull) {
lastDiskFullCheck = performance.now();
isDiskFull(
getDiskFullTrigger(getState()),
getSessionRootFolder(getState())
).then(isFull => {
if (isFull) {
logger.warn(
'Session stopped. Disk full trigger detected'
);
dispatch(samplingStop());
}
});
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/SidePanel/SessionSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default () => {
unit="MB"
value={diskFullTrigger}
range={{
min: 1,
min: 10, // 4 * 100_000 * 10 bytes ~= 5.7 MB per page
max: 10240,
decimals: undefined,
step: undefined,
Expand Down
32 changes: 29 additions & 3 deletions src/components/SidePanel/StartStop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
* SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
*/

import React, { useEffect, useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import {
ConfirmationDialog,
Group,
logger,
StartStopButton,
telemetry,
} from '@nordicsemiconductor/pc-nrfconnect-shared';
Expand Down Expand Up @@ -56,6 +57,8 @@ const calcFileSizeString = (sampleFreq: number, durationSeconds: number) => {

export default () => {
const dispatch = useDispatch();

const onWriteListener = useRef<() => void>();
const scopePane = useSelector(isScopePane);
const dataLoggerPane = useSelector(isDataLoggerPane);
const recordingMode = useSelector(getRecordingMode);
Expand Down Expand Up @@ -88,8 +91,30 @@ export default () => {
mode,
samplesPerSecond: DataManager().getSamplesPerSecond(),
});
dispatch(samplingStart());

const space = Math.max(
0,
await getFreeSpace(diskFullTrigger, sessionFolder)
);

setFreeSpace(space);

if (space === 0) {
logger.warn(
'Disk is full. Unable to start sampling. Change the disk full trigger threshold or free up disk memory.'
);
setShowDialog(false);
return;
}

setShowDialog(false);
await dispatch(samplingStart());
onWriteListener.current?.();
onWriteListener.current = DataManager().onFileWrite(() => {
getFreeSpace(diskFullTrigger, sessionFolder).then(s => {
setFreeSpace(Math.max(0, s));
});
});
};

const [freeSpace, setFreeSpace] = useState<number>(0);
Expand All @@ -99,7 +124,7 @@ export default () => {

const action = () => {
getFreeSpace(diskFullTrigger, sessionFolder).then(space => {
setFreeSpace(space);
setFreeSpace(Math.max(0, space));
});
};
action();
Expand Down Expand Up @@ -128,6 +153,7 @@ export default () => {
stopText="Stop"
onClick={async () => {
if (samplingRunning) {
onWriteListener.current?.();
dispatch(samplingStop());
telemetry.sendEvent('StopSampling', {
mode: recordingMode,
Expand Down
2 changes: 2 additions & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ export const DataManager = () => ({
});
}),
hasPendingTriggers: () => options.timeReachedTriggers.length > 0,
onFileWrite: (listener: () => void) =>
options.fileBuffer?.onFileWrite(listener),
});

/**
Expand Down
25 changes: 23 additions & 2 deletions src/utils/FileBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class FileBuffer {
#fileBusy = false;
#beforeUnload: (event: BeforeUnloadEvent) => Promise<void>;
#bufferingListeners: ((event: Promise<void>) => void)[] = [];
#fileWriteListeners: (() => void)[] = [];
#freePageBuffers: Uint8Array[] = [];
#bufferingRequests: Promise<void>[] = [];
#cancelBufferOperations: WeakMap<Promise<void>, AbortController> =
Expand Down Expand Up @@ -125,7 +126,10 @@ export class FileBuffer {
throw new Error('Invalid File handle');
return fs
.appendFile(this.#fileHandle, activePage.page)
.finally(resolve);
.finally(() => {
this.#fileWriteListeners.forEach(l => l());
resolve();
});
});
} else {
this.fileOperationTasks.push(() => {
Expand All @@ -137,7 +141,10 @@ export class FileBuffer {
this.#fileHandle,
activePage.page.subarray(0, activePage.bytesWritten)
)
.finally(resolve);
.finally(() => {
this.#fileWriteListeners.forEach(l => l());
resolve();
});
});

writeBuffer.switchPage();
Expand Down Expand Up @@ -577,4 +584,18 @@ export class FileBuffer {
}
};
}

onFileWrite(listener: () => void) {
this.#fileWriteListeners.push(listener);

return () => {
const index = this.#fileWriteListeners.findIndex(
l => l === listener
);

if (index !== -1) {
this.#fileWriteListeners.splice(index, 1);
}
};
}
}

0 comments on commit 31a85a0

Please sign in to comment.