Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Python OS Metrics Collection
00:00
5 left

Python OS Metrics Collection

HardPython

Problem

An NVIDIA DGX node exposes cumulative CPU counters in /proc/stat and memory values in /proc/meminfo. Given ordered Linux metric snapshots, calculate the CPU utilization and memory utilization for each interval between consecutive snapshots.

Formal Specification

Implement collect_linux_metrics(snapshots), where snapshots is a list of dictionaries. Each dictionary contains:

  • timestamp: an integer or float identifying the sample time.
  • stat: the /proc/stat text containing a line beginning with cpu followed by cumulative counters.
  • meminfo: the /proc/meminfo text containing MemTotal and MemAvailable, both measured in kB.

Return a list of dictionaries, one for every snapshot after the first. Each result must contain the later snapshot's timestamp, cpu_percent, and memory_percent. Round both percentages to two decimal places.

CPU utilization is the percentage of elapsed CPU time that was not idle. Treat both idle and iowait as idle time. Memory utilization is (MemTotal - MemAvailable) / MemTotal * 100.

Constraints

  • 2 <= len(snapshots) <= 10^4
  • CPU counters are nonnegative cumulative integers.
  • Each snapshot contains valid cpu, MemTotal, and MemAvailable values.
  • MemTotal > 0 and 0 <= MemAvailable <= MemTotal.
  • Snapshot timestamps are in nondecreasing order.

Function Signature

def collect_linux_metrics(snapshots):
Interviewer

Your question is Python OS Metrics Collection. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.