#!/bin/sh
# Copyright (C) HITACHI,LTD. 2005
# WRITTEN BY HITACHI SYSTEMS DEVELOPMENT LABORATORY,
# Created by M.Hiramatsu <hiramatu@sdl.hitachi.co.jp>


[ $# -le 1 ] && exit 0

LKSTM=/usr/sbin/lkstm
LKSTEH=/usr/sbin/lksteh

MNAME=$1
shift 1

function findeid() {
    grep ",$1," /proc/lkst_etypes | cut -f1 -d,
}

# configmask maskname eventname eventhandlerid
function configmask () {
    local EN=`findeid $2`
    [ "$EN" ] && $LKSTM config -n $1 $EN $3
}

function exception() {
    configmask $MNAME EXCEPTION_ENTRY 0x01
    configmask $MNAME EXCEPTION_EXIT 0x01
}

function biotime() {
    configmask $MNAME BIO_MAKE_REQ 0x01
    configmask $MNAME BIO_END_IO 0x01
}

function biomake() {
    configmask $MNAME BIO_MAKE_REQ 0x01
    configmask $MNAME BIO_MAKE_REQ2 0x01
}

function blkqueue() {
    configmask $MNAME BLK_GET_REQ 0x01
    configmask $MNAME BLK_PUT_REQ 0x01
}
                                                                                
function bioqueue() {
    biotime
    blkqueue
}

function elvtime() {
    configmask $MNAME BIO_MAKE_REQ2 0x01
    configmask $MNAME ELV_NEXT_REQ 0x01
}

function elvqueue() {
    elvtime
    blkqueue
}

function bdevtime() {
    elvtime
    configmask $MNAME BIO_END_IO 0x01
    configmask $MNAME BLK_PUT_REQ 0x01
}

function iosector () {
    configmask $MNAME ELV_NEXT_REQ 0x01
}

function buffer() {
    configmask $MNAME BUFFER_SUBMIT_BH 0x01
    configmask $MNAME BUFFER_ENDIO_BH 0x01
}

function busywait() {
    configmask $MNAME LK_SPINLOCK 0x01
}

function netroute() {
    configmask $MNAME NET_V4RTOUT_ENTER 0x01
    configmask $MNAME NET_V4RTOUT_EXIT 0x01
    configmask $MNAME NET_V4RTIN_ENTER 0x01
    configmask $MNAME NET_V4RTIN_EXIT 0x01
}

function netsend() {
    configmask $MNAME NET_PKTSEND 0x01
    configmask $MNAME NET_START_XMIT 0x01
}

function palloc() {
    configmask $MNAME PAGE_ALLOC_ENTER 0x01
    configmask $MNAME PAGE_ALLOC_EXIT 0x01
}

function proclive() {
    configmask $MNAME PROCESS_FORK 0x01
    configmask $MNAME PROCESS_EXIT 0x01
    configmask $MNAME PROCESS_SCHED_ENTER 0x01
}

function procstat() {
    configmask $MNAME PROCESS_FORK 0x01
    configmask $MNAME PROCESS_EXIT 0x01
    configmask $MNAME PROCESS_SCHED_ENTER 0x01
    PSTAT=`$LKSTEH find -n procstat| cut -f1 -d\  `
    if [ $PSTAT -eq 255 ]; then 
	echo "Failed to find procstat handler"
	exit -1;
    fi
    configmask $MNAME PROCESS_CONTEXTSWITCH $PSTAT
    configmask $MNAME PROCESS_WAKEUP $PSTAT
}

function runqueue() {
    PSTAT=`$LKSTEH find -n procstat| cut -f1 -d\  `
    if [ "$PSTAT" -eq 255 ]; then 
	PSTAT=1
    fi
    configmask $MNAME PROCESS_CONTEXTSWITCH $PSTAT
}

function schedrun() {
    configmask $MNAME PROCESS_SCHED_ENTER 0x01
    configmask $MNAME PROCESS_SCHED_EXIT 0x01
}

function schedule() {
    configmask $MNAME PROCESS_SCHED_ENTER 0x01
    configmask $MNAME PROCESS_SCHED_EXIT 0x01
}

function softirq() {
    configmask $MNAME SOFTIRQ_RAISE 0x01
    configmask $MNAME SOFTIRQ_ACTION 0x01
}

function spinlock() {
    configmask $MNAME LK_SPINTRYLOCK 0x01
    configmask $MNAME LK_SPINLOCK 0x01
    configmask $MNAME LK_SPINUNLOCK 0x01
}

function syscall() {
    SI=`$LKSTEH find -n sysinfo| cut -f1 -d\  `
    if [ $SI -eq 255 ]; then 
	SI=1;
    fi
    configmask $MNAME SYSCALL_SYSENTER $SI
    configmask $MNAME SYSCALL_ENTRY $SI
    configmask $MNAME SYSCALL_EXIT 0x01
    configmask $MNAME SYSCALL_SYSEXIT 0x01
    PSTAT=`$LKSTEH find -n procstat| cut -f1 -d\  `
    if [ $PSTAT -eq 255 ]; then 
	echo "Failed to find procstat handler"
	exit -1;
    fi
    configmask $MNAME PROCESS_CONTEXTSWITCH $PSTAT
}

function timer() {
    configmask $MNAME TIMER_RUN 0x01
}

function vminfo() {
    VI=`$LKSTEH find -n vminfo | cut -f1 -d\  `
    if [ $VI -eq 255 ]; then 
	echo "Failed to find vminfo handler"
	exit -1;
    fi
    configmask $MNAME SHRINK_CACHE_ENTER $VI
    configmask $MNAME PAGE_AGING_ENTER $VI
}

function vmscan() {
    configmask $MNAME SHRINK_SLAB_ENTER 0x01
    configmask $MNAME SHRINK_CACHE_ENTER 0x01
    configmask $MNAME PAGE_AGING_ENTER 0x01
    configmask $MNAME PAGE_RECLAIM_ENTER 0x01
    configmask $MNAME SHRINK_SLAB_EXIT 0x01
    configmask $MNAME SHRINK_CACHE_EXIT 0x01
    configmask $MNAME PAGE_AGING_EXIT 0x01
    configmask $MNAME PAGE_RECLAIM_EXIT 0x01
}

function waitqueue() {
    WQ=`$LKSTEH find -n wqcounter| cut -f1 -d\  `
    if [ $WQ -eq 255 ]; then 
	echo "Failed to find wqcounter handler"
	exit -1;
    fi
    configmask $MNAME PROCESS_ADD_WQ $WQ
}

function waittime() {
    WQ=`$LKSTEH find -n wqcounter| cut -f1 -d\  `
    if [ $WQ -eq 255 ]; then 
	WQ=1
    fi
    configmask $MNAME PROCESS_ADD_WQ $WQ
    configmask $MNAME PROCESS_REM_WQ 0x01
}

function sysinfo() {
    SI=`$LKSTEH find -n sysinfo| cut -f1 -d\  `
    if [ $SI -eq 255 ]; then 
	echo "Failed to find sysinfo handler"
	exit -1;
    fi
    configmask $MNAME SYSCALL_SYSENTER $SI
    configmask $MNAME SYSCALL_ENTRY $SI
}

if [ ! -e /dev/lkst ]; then
    echo "LKST device /dev/lkst doesn't exist"
    exit 1;
fi

#make a new blank maskset
$LKSTM read -m 0 -a | $LKSTM write -n $MNAME
for i in $*; do
$i
done
