Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom events for utilization log #103

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions python/dendro/sdk/_resource_utilization_log_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def _resource_utilization_log_reader(outq: queue.Queue, exit_event: threading.Ev
net_io_counters = psutil.net_io_counters(pernic=False, nowrap=True)
gpu_loads = _get_gpu_loads()
log_record = {
'type': 'utilization_event',
'timestamp': time.time(),
'cpu': {
'percent': cpu_percent
Expand Down
40 changes: 40 additions & 0 deletions python/dendro/sdk/_run_job_parent_process.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import os
import json
from typing import Union, Optional, Dict, Any
import threading
import queue
Expand Down Expand Up @@ -53,11 +54,26 @@ def _run_job_parent_process(*, job_id: str, job_private_key: str, app_executable
resource_utilization_log_upload_url: Union[str, None] = None
resource_utilization_log_upload_url_timestamp = 0

# detect custom events in the console output
# of the form ##dendro-event##{...}
def on_detect_custom_event(evt):
nonlocal all_resource_utilization_log
nonlocal resource_utilization_log_changed
log_record = {
'type': 'custom_event',
'timestamp': time.time(),
'event': evt
}
all_resource_utilization_log += (json.dumps(log_record) + '\n').encode('utf-8') # important to encode this string
resource_utilization_log_changed = True
custom_event_detector = CustomEventDetector(on_detect_custom_event)

def check_for_new_console_output():
nonlocal console_out_q
nonlocal last_newline_index_in_console_output
nonlocal all_console_output
nonlocal console_output_changed
nonlocal custom_event_detector
while True:
try:
# get the latest output from the job
Expand All @@ -69,6 +85,7 @@ def check_for_new_console_output():
# handle carriage return (e.g. in progress bar)
all_console_output = all_console_output[:last_newline_index_in_console_output + 1]
all_console_output += x
custom_event_detector.process_bytes(x)
console_output_changed = True
except queue.Empty:
break
Expand Down Expand Up @@ -471,3 +488,26 @@ def _debug_log(msg: str):
timestamp_str = time.strftime('%Y-%m-%d %H:%M:%S')
with open('dendro-job.log', 'a', encoding='utf-8') as f:
f.write(f'{timestamp_str} {msg}\n')

# Look for the events of the form ##dendro-event##{...} in the console output
class CustomEventDetector:
def __init__(self, on_detect_custom_event):
self._on_detect_custom_event = on_detect_custom_event
self._last_line = b''
def process_bytes(self, x: bytes):
for i in range(len(x)):
self._process_byte(x[i:i + 1])
def _process_byte(self, x: bytes):
if x == b'\n':
self._process_line(self._last_line)
self._last_line = b''
else:
self._last_line += x
def _process_line(self, line: bytes):
if line.startswith(b'##dendro-event##'):
evt_json = line[len(b'##dendro-event##'):]
try:
evt = json.loads(evt_json)
self._on_detect_custom_event(evt)
except: # noqa: E722
pass
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type ResourceUtilizationViewProps = {
type ResourceUtilizationLog = ResourceUtilizationLogLine[]

type ResourceUtilizationLogLine = {
type: 'utilization_event',
timestamp: number,
cpu: {
percent: number
Expand Down Expand Up @@ -48,6 +49,10 @@ type ResourceUtilizationLogLine = {
gpu: {
loads: number[]
} | null
} | {
type: 'custom_event',
timestamp: number,
event: any
}

const useResourceUtilizationLog = (job: DendroJob) => {
Expand Down Expand Up @@ -97,9 +102,13 @@ const ResourceUtilizationView: FunctionComponent<ResourceUtilizationViewProps> =
const {resourceUtilizationLog, refreshResourceUtilizationLog} = useResourceUtilizationLog(job)
const referenceTime = resourceUtilizationLog && resourceUtilizationLog.length > 0 ? resourceUtilizationLog[0].timestamp : 0

const resourceUtilizationLogFiltered: (ResourceUtilizationLogLine & {type: 'utilization_event'})[] = useMemo(() => {
return resourceUtilizationLog.filter(l => l.type === 'utilization_event') as any
}, [resourceUtilizationLog])

const handleDownloadCsv = useCallback(() => {
const headerLine = "timestamp,cpu_percent,memory_used,memory_total,network_sent,network_received,disk_read,disk_write"
const lines = resourceUtilizationLog.map(l => {
const lines = resourceUtilizationLogFiltered.map(l => {
const cpuPercent = l.cpu.percent
const memoryUsed = l.virtual_memory.used / 1024 / 1024 / 1024
const memoryTotal = l.virtual_memory.total / 1024 / 1024 / 1024
Expand All @@ -117,7 +126,7 @@ const ResourceUtilizationView: FunctionComponent<ResourceUtilizationViewProps> =
a.href = url
a.download = `resource_utilization_${job.jobId}.csv`
a.click()
}, [resourceUtilizationLog, job])
}, [resourceUtilizationLogFiltered, job])

const handleDownloadJsonl = useCallback(() => {
const lines = resourceUtilizationLog.map(l => JSON.stringify(l))
Expand Down Expand Up @@ -145,7 +154,7 @@ const ResourceUtilizationView: FunctionComponent<ResourceUtilizationViewProps> =
series={[
{
label: 'CPU percent',
data: resourceUtilizationLog.map(l => ({
data: resourceUtilizationLogFiltered.map(l => ({
x: l.timestamp,
y: l.cpu.percent
})),
Expand All @@ -160,15 +169,15 @@ const ResourceUtilizationView: FunctionComponent<ResourceUtilizationViewProps> =
series={[
{
label: 'Memory used',
data: resourceUtilizationLog.map(l => ({
data: resourceUtilizationLogFiltered.map(l => ({
x: l.timestamp,
y: l.virtual_memory.used / 1024 / 1024 / 1024
})),
color: 'red'
},
{
label: 'Total memory',
data: resourceUtilizationLog.map(l => ({
data: resourceUtilizationLogFiltered.map(l => ({
x: l.timestamp,
y: l.virtual_memory.total / 1024 / 1024 / 1024
})),
Expand All @@ -183,7 +192,7 @@ const ResourceUtilizationView: FunctionComponent<ResourceUtilizationViewProps> =
series={[
{
label: 'GPU load',
data: resourceUtilizationLog.map(l => ({
data: resourceUtilizationLogFiltered.map(l => ({
x: l.timestamp,
y: l.gpu ? l.gpu.loads.reduce((a, b) => a + b, 0) : 0
})),
Expand All @@ -199,15 +208,15 @@ const ResourceUtilizationView: FunctionComponent<ResourceUtilizationViewProps> =
series={[
{
label: 'Network sent',
data: resourceUtilizationLog.map(l => ({
data: resourceUtilizationLogFiltered.map(l => ({
x: l.timestamp,
y: (l.net_io_counters?.bytes_sent || 0) / 1024 / 1024 / 1024
})),
color: 'blue'
},
{
label: 'Network received',
data: resourceUtilizationLog.map(l => ({
data: resourceUtilizationLogFiltered.map(l => ({
x: l.timestamp,
y: (l.net_io_counters?.bytes_recv || 0) / 1024 / 1024 / 1024
})),
Expand All @@ -222,15 +231,15 @@ const ResourceUtilizationView: FunctionComponent<ResourceUtilizationViewProps> =
series={[
{
label: 'Network sent',
data: cumulativeToInstantaneous(resourceUtilizationLog.map(l => ({
data: cumulativeToInstantaneous(resourceUtilizationLogFiltered.map(l => ({
x: l.timestamp,
y: (l.net_io_counters?.bytes_sent || 0) / 1024 / 1024
}))),
color: 'blue'
},
{
label: 'Network received',
data: cumulativeToInstantaneous(resourceUtilizationLog.map(l => ({
data: cumulativeToInstantaneous(resourceUtilizationLogFiltered.map(l => ({
x: l.timestamp,
y: (l.net_io_counters?.bytes_recv || 0) / 1024 / 1024
}))),
Expand All @@ -247,15 +256,15 @@ const ResourceUtilizationView: FunctionComponent<ResourceUtilizationViewProps> =
series={[
{
label: 'Disk read',
data: resourceUtilizationLog.map(l => ({
data: resourceUtilizationLogFiltered.map(l => ({
x: l.timestamp,
y: (l.disk_io_counters?.read_bytes || 0) / 1024 / 1024
})),
color: 'darkgreen'
},
{
label: 'Disk write',
data: resourceUtilizationLog.map(l => ({
data: resourceUtilizationLogFiltered.map(l => ({
x: l.timestamp,
y: (l.disk_io_counters?.write_bytes || 0) / 1024 / 1024
})),
Expand All @@ -270,15 +279,15 @@ const ResourceUtilizationView: FunctionComponent<ResourceUtilizationViewProps> =
series={[
{
label: 'Disk read',
data: cumulativeToInstantaneous(resourceUtilizationLog.map(l => ({
data: cumulativeToInstantaneous(resourceUtilizationLogFiltered.map(l => ({
x: l.timestamp,
y: (l.disk_io_counters?.read_bytes || 0) / 1024 / 1024
}))),
color: 'darkgreen'
},
{
label: 'Disk write',
data: cumulativeToInstantaneous(resourceUtilizationLog.map(l => ({
data: cumulativeToInstantaneous(resourceUtilizationLogFiltered.map(l => ({
x: l.timestamp,
y: (l.disk_io_counters?.write_bytes || 0) / 1024 / 1024
}))),
Expand Down