Using the kstat Interface to Get Idle Time
by Gopinath Rao
(April 2002)
We want to hear from you! Send us your
FEEDBACK.
The following codesample may contain actual software programs in source
code form. This source code is made available for developers to use as
needed, pursuant to the terms and conditions of this
license.
Overview:
Users have experienced a problem with power management in the Solaris[tm] 8 Operating Environment (OE),
update 6 (10/01). When they try to get the device idle time on update 5, they have no trouble.
However, on update 6 their code fails. This code makes use of the power management
ioctl PM_GET_TIME_IDLE to read the idle time from the consms and
conskbd driver modules.
Sample Program:
Starting with the Solaris 8 OE, update 6 (10/01), use the kstat
interface to get the idle time for the keyboard and mouse.
See the following idle.c sample program:
/*
* This program calculates the idle time of a keyboard and mouse using
* kstat interface.
*
* The program can be used on Solaris 8 Update 6 (10/01) and above only.
*
* The program needs to be compiled as:
*
* cc -o idle -lkstat idle.c
*
* Invoke the program binary as :./idle
*
* The program output is idle time of keyboard and mouse in seconds.
*
*/
#include <kstat.h>
#include <time.h>
static kstat_ctl_t *kc; /* libkstat cookie */
static kstat_t *kbd_ksp;
static kstat_t *ms_ksp;
long kbd_idle_time(void);
static void kbd_init(void);
static void ms_init(void);
long ms_idle_time(void);
/* Returns the number of seconds since the last keystroke on console keyboard */
long
kbd_idle_time(void)
{
void *p;
if (kbd_ksp == NULL || kstat_read(kc, kbd_ksp, NULL) == -1 ||
(p = kstat_data_lookup(kbd_ksp, "idle_sec")) == NULL){
return ((time_t)-1);
}
return (((kstat_named_t *)p)->value.l);
}
/* Returns the number of seconds since the most recent movement or click on console mouse */
long
ms_idle_time(void)
{
void *p;
if(ms_ksp == NULL || kstat_read(kc,ms_ksp,NULL) == -1 || (p = kstat_data_lookup(ms_ksp, "idle_sec"))
== NULL) {
return((time_t)-1);
}
return(((kstat_named_t *)p)-> value.l);
}
static void
kbd_init(void)
{
kbd_ksp = kstat_lookup(kc, "conskbd", 0, "activity");
if(kbd_ksp == NULL){
printf("Keyboard init failed\n");
exit(-1);
}
}
static void
ms_init(void)
{
ms_ksp = kstat_lookup(kc, "consms", 0, "activity");
if(ms_ksp == NULL){
printf("Mouse init failed\n");
exit(-1);
}
}
main()
{
int kbd;
int ms;
/* kstat_open() will initialize a kstat control structure */
if ((kc = kstat_open()) == NULL) {
printf("kstat_open(): can't open /dev/kstat\n");
exit(-1);
}
kbd_init();
ms_init();
if((kbd = (int)kbd_idle_time()) < 0){
printf("failed to get idle time for console keyboard\n");
exit(-1);
}
printf("Idle time of keyboard>:%d\n",kbd);
if((ms = (int)ms_idle_time()) < 0){
printf("failed to get idle time for console mouse\n");
exit(-1);
}
printf("Idle time of mouse>:%d\n",ms);
kstat_close(kc);
}
PROGRAM EXECUTION:
Some idle time testing scenarios are given below:
CASE A:
When a user is executing this program on the console and using only
the keyboard (mouse being idle):
% ./idle
Idle time of keyboard>:0
Idle time of mouse>:20
CASE B:
When a user is executing this program on the console and simultaneously
moving the mouse pointer:
% ./idle
Idle time of keyboard>:0
Idle time of mouse>:0
CASE C:
When a user is remotely logged into a system on which both the
keyboard and mouse are idle for sometime:
% ./idle
Idle time of keyboard>:124
Idle time of mouse>:124
REFERENCES:
Man pages for kstat(3KSTAT)
|