#!/usr/bin/env bash
amdgpu_top -J -s 5000 | jq -c --unbuffered '.devices[0] | {
  gfx_pct:   (.gpu_activity.GFX.value // 0),
  mem_pct:   (.gpu_activity.Memory.value // 0),
  media_pct: (.gpu_activity.MediaEngine.value // 0),
  sclk:      (.Sensors.GFX_SCLK.value // 0),
  mclk:      (.Sensors.GFX_MCLK.value // 0),
  sclk_pct:  (if (.Info["GPU Clock"].max != .Info["GPU Clock"].min) then
                100 * ((.Sensors.GFX_SCLK.value // 0) - .Info["GPU Clock"].min) / (.Info["GPU Clock"].max - .Info["GPU Clock"].min)
              else 0 end),
  mclk_pct:  (if (.Info["Memory Clock"].max != .Info["Memory Clock"].min) then
                100 * ((.Sensors.GFX_MCLK.value // 0) - .Info["Memory Clock"].min) / (.Info["Memory Clock"].max - .Info["Memory Clock"].min)
              else 0 end),
  vclk:      (.gpu_metrics.average_vclk_frequency // 0),
  vclk_pct:  (if (.Info["GPU Clock"].max > 0) then
                100 * (.gpu_metrics.average_vclk_frequency // 0) / .Info["GPU Clock"].max
              else 0 end),
  temp:       (.Sensors["Edge Temperature"].value // 0),
  power:      (.Sensors["Average Power"].value // 0),
  vram_used:  (.VRAM["Total VRAM Usage"].value // 0),
  vram_total: (.VRAM["Total VRAM"].value // 1)
}'
