#!/bin/sh
## Generate some metadata that we use to help parse our ktr save files.
##
## If --cat is specified, after the metadata, copy from stdin to stdout. This
## allows for ktrmeta to be used as:
##   ktrdump <args> | ktrmeta --cat > file
## to prepend the metadata to the ktrdump

DO_CAT=0
if [ $# -eq 1 -a "X"$1 = "X--cat" ] ; then
    DO_CAT=1
elif [ $# -ne 0 ] ; then
    echo "Usage: $0 [--cat]"
    exit 2
fi

## Arbitrary versioning
echo "# ktrmeta.version: 1"

## Drive bay / lnum / isi-unit / ifs partition offset / device-name mappings
#  XXX: Note this doesn't work properly on virtual nodes due to bug 152492,
#  but... it's still the right way to do it.
isi_drivenum -buliD `sysctl -n kern.disks` | awk '{ print "# disk.info: " $0 ; }'

## Clock source mappings, for frequency, boot-time, and now-time
## * Note that we don't know if TSC or TSC-low exists, but by just squelching
##   stderr we only get the one(s) that exist. [2>&- closes fd2]
## * Regarding TSC-low. If TSC is the timecounter, it's a direct match to the
##   cpu tsc. But if that tsc frequency is >2GHz, BSD starts shifting it down,
##   and publishes it as TSC-low. It doesn't tell us what it shifts by (and our
##   ktr logs are in unshifted cpu tsc counts, so we really really want to
##   know), but we can derive that by comparing the tsc frequency to the cpu
##   frequency stated in the hw.model string.
sysctl hw.model kern.clockrate kern.boottime \
       kern.timecounter.tc.TSC.frequency kern.timecounter.tc.TSC-low.frequency \
       kern.timecounter.tc.TSC.counter kern.timecounter.tc.TSC-low.counter \
       2>&- | awk '{ print "# " $0 ; }'
date -j +"# time.now: { sec = %s } %c"

## End of meta, so now copy from stdin to stdout if requested
if [ $DO_CAT -eq 1 ] ; then
    cat
fi

## done
