[BACK]Return to sensors.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / systat

Annotation of src/usr.bin/systat/sensors.c, Revision 1.22

1.22    ! deraadt     1: /*     $OpenBSD: sensors.c,v 1.21 2010/04/20 20:49:35 deraadt Exp $    */
1.8       deanna      2:
1.1       deanna      3: /*
                      4:  * Copyright (c) 2007 Deanna Phillips <deanna@openbsd.org>
                      5:  * Copyright (c) 2003 Henning Brauer <henning@openbsd.org>
1.4       deraadt     6:  * Copyright (c) 2006 Constantine A. Murenin <cnst+openbsd@bugmail.mojo.ru>
1.1       deanna      7:  *
                      8:  * Permission to use, copy, modify, and distribute this software for any
                      9:  * purpose with or without fee is hereby granted, provided that the above
                     10:  * copyright notice and this permission notice appear in all copies.
                     11:  *
                     12:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     13:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     14:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     15:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     16:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     17:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     18:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     19:  *
                     20:  */
                     21:
                     22: #include <sys/param.h>
                     23: #include <sys/sysctl.h>
                     24: #include <sys/sensors.h>
                     25:
                     26: #include <err.h>
                     27: #include <errno.h>
                     28: #include <stdio.h>
                     29: #include <stdlib.h>
1.13      canacar    30: #include <string.h>
1.1       deanna     31: #include "systat.h"
                     32:
                     33: struct sensor sensor;
                     34: struct sensordev sensordev;
                     35:
1.13      canacar    36: struct sensinfo {
                     37:        int sn_dev;
                     38:        struct sensor sn_sensor;
                     39: };
                     40: #define sn_type sn_sensor.type
                     41: #define sn_numt sn_sensor.numt
                     42: #define sn_desc sn_sensor.desc
                     43: #define sn_status sn_sensor.status
                     44: #define sn_value sn_sensor.value
                     45:
1.21      deraadt    46: #define SYSTAT_MAXSENSORDEVICES 1024
                     47: char *devnames[SYSTAT_MAXSENSORDEVICES];
1.13      canacar    48:
                     49: #define ADD_ALLOC 100
                     50: static size_t sensor_cnt = 0;
                     51: static size_t num_alloc = 0;
                     52: static struct sensinfo *sensors = NULL;
                     53:
                     54: static char *fmttime(double);
                     55: static void showsensor(struct sensinfo *s);
                     56:
                     57: void print_sn(void);
                     58: int read_sn(void);
                     59: int select_sn(void);
                     60:
                     61: const char *drvstat[] = {
                     62:        NULL,
1.16      okan       63:        "empty", "ready", "powering up", "online", "idle", "active",
                     64:        "rebuilding", "powering down", "failed", "degraded"
1.13      canacar    65: };
                     66:
                     67:
                     68: field_def fields_sn[] = {
                     69:        {"SENSOR", 16, 32, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0},
                     70:        {"VALUE", 16, 20, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
                     71:        {"STATUS", 5, 8, 1, FLD_ALIGN_CENTER, -1, 0, 0, 0},
                     72:        {"DESCRIPTION", 20, 45, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0}
                     73: };
                     74:
                     75: #define FIELD_ADDR(x) (&fields_sn[x])
                     76:
                     77: #define FLD_SN_SENSOR  FIELD_ADDR(0)
                     78: #define FLD_SN_VALUE   FIELD_ADDR(1)
                     79: #define FLD_SN_STATUS  FIELD_ADDR(2)
                     80: #define FLD_SN_DESCR   FIELD_ADDR(3)
                     81:
                     82: /* Define views */
                     83: field_def *view_sn_0[] = {
                     84:        FLD_SN_SENSOR, FLD_SN_VALUE, FLD_SN_STATUS, FLD_SN_DESCR, NULL
                     85: };
                     86:
                     87:
                     88: /* Define view managers */
                     89: struct view_manager sensors_mgr = {
                     90:        "Sensors", select_sn, read_sn, NULL, print_header,
                     91:        print_sn, keyboard_callback, NULL, NULL
                     92: };
                     93:
                     94: field_view views_sn[] = {
                     95:        {view_sn_0, "sensors", '3', &sensors_mgr},
                     96:        {NULL, NULL, 0, NULL}
                     97: };
                     98:
                     99: struct sensinfo *
                    100: next_sn(void)
1.1       deanna    101: {
1.13      canacar   102:        if (num_alloc <= sensor_cnt) {
                    103:                struct sensinfo *s;
                    104:                size_t a = num_alloc + ADD_ALLOC;
                    105:                if (a < num_alloc)
                    106:                        return NULL;
                    107:                s = realloc(sensors, a * sizeof(struct sensinfo));
                    108:                if (s == NULL)
                    109:                        return NULL;
                    110:                sensors = s;
                    111:                num_alloc = a;
                    112:        }
                    113:
                    114:        return &sensors[sensor_cnt++];
1.1       deanna    115: }
                    116:
                    117:
1.13      canacar   118: int
                    119: select_sn(void)
1.1       deanna    120: {
1.13      canacar   121:        num_disp = sensor_cnt;
                    122:        return (0);
1.1       deanna    123: }
                    124:
1.13      canacar   125: int
                    126: read_sn(void)
1.1       deanna    127: {
                    128:        enum sensor_type type;
                    129:        size_t           slen, sdlen;
                    130:        int              mib[5], dev, numt;
1.13      canacar   131:        struct sensinfo *s;
1.1       deanna    132:
                    133:        mib[0] = CTL_HW;
                    134:        mib[1] = HW_SENSORS;
                    135:
                    136:        sensor_cnt = 0;
1.8       deanna    137:
1.21      deraadt   138:        for (dev = 0; dev < SYSTAT_MAXSENSORDEVICES; dev++) {
1.1       deanna    139:                mib[2] = dev;
1.13      canacar   140:                sdlen = sizeof(struct sensordev);
1.1       deanna    141:                if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
1.21      deraadt   142:                        if (errno == ENOENT)
                    143:                                break;
                    144:                        if (errno == ENXIO)
                    145:                                continue;
                    146:                        error("sysctl: %s", strerror(errno));
1.1       deanna    147:                }
1.13      canacar   148:
                    149:                if (devnames[dev] && strcmp(devnames[dev], sensordev.xname)) {
                    150:                        free(devnames[dev]);
                    151:                        devnames[dev] = NULL;
                    152:                }
                    153:                if (devnames[dev] == NULL)
                    154:                        devnames[dev] = strdup(sensordev.xname);
                    155:
1.1       deanna    156:                for (type = 0; type < SENSOR_MAX_TYPES; type++) {
                    157:                        mib[3] = type;
                    158:                        for (numt = 0; numt < sensordev.maxnumt[type]; numt++) {
                    159:                                mib[4] = numt;
1.13      canacar   160:                                slen = sizeof(struct sensor);
1.2       deraadt   161:                                if (sysctl(mib, 5, &sensor, &slen, NULL, 0)
1.1       deanna    162:                                    == -1) {
                    163:                                        if (errno != ENOENT)
1.13      canacar   164:                                                error("sysctl: %s", strerror(errno));
1.1       deanna    165:                                        continue;
                    166:                                }
                    167:                                if (sensor.flags & SENSOR_FINVALID)
                    168:                                        continue;
1.13      canacar   169:
                    170:                                s = next_sn();
                    171:                                s->sn_sensor = sensor;
                    172:                                s->sn_dev = dev;
1.1       deanna    173:                        }
                    174:                }
                    175:        }
1.13      canacar   176:
                    177:        num_disp = sensor_cnt;
                    178:        return 0;
1.1       deanna    179: }
                    180:
                    181:
                    182: void
1.13      canacar   183: print_sn(void)
1.1       deanna    184: {
1.13      canacar   185:        int n, count = 0;
                    186:
                    187:        for (n = dispstart; n < num_disp; n++) {
                    188:                showsensor(sensors + n);
                    189:                count++;
                    190:                if (maxprint > 0 && count >= maxprint)
                    191:                        break;
                    192:        }
1.1       deanna    193: }
                    194:
                    195: int
                    196: initsensors(void)
                    197: {
1.13      canacar   198:        field_view *v;
                    199:
                    200:        memset(devnames, 0, sizeof(devnames));
                    201:
                    202:        for (v = views_sn; v->name != NULL; v++)
                    203:                add_view(v);
                    204:
                    205:        return(1);
1.1       deanna    206: }
                    207:
1.13      canacar   208: static void
                    209: showsensor(struct sensinfo *s)
1.1       deanna    210: {
1.13      canacar   211:        tb_start();
                    212:        tbprintf("%s.%s%d", devnames[s->sn_dev],
                    213:                 sensor_type_s[s->sn_type], s->sn_numt);
                    214:        print_fld_tb(FLD_SN_SENSOR);
                    215:
                    216:        if (s->sn_desc[0] != '\0')
                    217:                print_fld_str(FLD_SN_DESCR, s->sn_desc);
                    218:
                    219:        tb_start();
                    220:
                    221:        switch (s->sn_type) {
1.1       deanna    222:        case SENSOR_TEMP:
1.13      canacar   223:                tbprintf("%10.2f degC",
                    224:                    (s->sn_value - 273150000) / 1000000.0);
1.1       deanna    225:                break;
                    226:        case SENSOR_FANRPM:
1.13      canacar   227:                tbprintf("%11lld RPM", s->sn_value);
1.1       deanna    228:                break;
                    229:        case SENSOR_VOLTS_DC:
1.13      canacar   230:                tbprintf("%10.2f V DC",
                    231:                    s->sn_value / 1000000.0);
1.18      cnst      232:                break;
                    233:        case SENSOR_WATTS:
                    234:                tbprintf("%10.2f W", s->sn_value / 1000000.0);
1.1       deanna    235:                break;
                    236:        case SENSOR_AMPS:
1.13      canacar   237:                tbprintf("%10.2f A", s->sn_value / 1000000.0);
1.1       deanna    238:                break;
                    239:        case SENSOR_INDICATOR:
1.13      canacar   240:                tbprintf("%15s", s->sn_value ? "On" : "Off");
1.1       deanna    241:                break;
                    242:        case SENSOR_INTEGER:
1.13      canacar   243:                tbprintf("%11lld raw", s->sn_value);
1.1       deanna    244:                break;
                    245:        case SENSOR_PERCENT:
1.13      canacar   246:                tbprintf("%14.2f%%", s->sn_value / 1000.0);
1.1       deanna    247:                break;
                    248:        case SENSOR_LUX:
1.13      canacar   249:                tbprintf("%15.2f lx", s->sn_value / 1000000.0);
1.1       deanna    250:                break;
                    251:        case SENSOR_DRIVE:
1.13      canacar   252:                if (0 < s->sn_value &&
1.17      deraadt   253:                    s->sn_value < sizeof(drvstat)/sizeof(drvstat[0])) {
1.13      canacar   254:                        tbprintf("%15s", drvstat[s->sn_value]);
1.1       deanna    255:                        break;
                    256:                }
1.3       deraadt   257:                break;
1.1       deanna    258:        case SENSOR_TIMEDELTA:
1.13      canacar   259:                tbprintf("%15s", fmttime(s->sn_value / 1000000000.0));
1.1       deanna    260:                break;
                    261:        case SENSOR_WATTHOUR:
1.13      canacar   262:                tbprintf("%12.2f Wh", s->sn_value / 1000000.0);
1.1       deanna    263:                break;
                    264:        case SENSOR_AMPHOUR:
1.13      canacar   265:                tbprintf("%10.2f Ah", s->sn_value / 1000000.0);
1.19      yuo       266:                break;
                    267:        case SENSOR_HUMIDITY:
                    268:                tbprintf("%3.2f%%", s->sn_value / 1000.0);
1.20      oga       269:                break;
                    270:        case SENSOR_FREQ:
                    271:                tbprintf("%11lld Hz", s->sn_value);
1.22    ! deraadt   272:                break;
        !           273:        case SENSOR_ANGLE:
        !           274:                tbprintf("%10lld deg", s->sn_value);
1.1       deanna    275:                break;
                    276:        default:
1.13      canacar   277:                tbprintf("%10lld", s->sn_value);
1.3       deraadt   278:                break;
1.1       deanna    279:        }
1.2       deraadt   280:
1.13      canacar   281:        print_fld_tb(FLD_SN_VALUE);
                    282:
                    283:        switch (s->sn_status) {
1.8       deanna    284:        case SENSOR_S_UNSPEC:
                    285:                break;
1.1       deanna    286:        case SENSOR_S_UNKNOWN:
1.13      canacar   287:                print_fld_str(FLD_SN_STATUS, "unknown");
1.1       deanna    288:                break;
                    289:        case SENSOR_S_WARN:
1.13      canacar   290:                print_fld_str(FLD_SN_STATUS, "WARNING");
1.1       deanna    291:                break;
                    292:        case SENSOR_S_CRIT:
1.13      canacar   293:                print_fld_str(FLD_SN_STATUS, "CRITICAL");
1.1       deanna    294:                break;
1.3       deraadt   295:        case SENSOR_S_OK:
1.13      canacar   296:                print_fld_str(FLD_SN_STATUS, "OK");
1.1       deanna    297:                break;
                    298:        }
1.13      canacar   299:        end_line();
1.9       ckuethe   300: }
                    301:
                    302: #define SECS_PER_DAY 86400
                    303: #define SECS_PER_HOUR 3600
                    304: #define SECS_PER_MIN 60
                    305:
                    306: static char *
                    307: fmttime(double in)
                    308: {
                    309:        int signbit = 1;
                    310:        int tiny = 0;
                    311:        char *unit;
                    312: #define LEN 32
                    313:        static char outbuf[LEN];
                    314:
                    315:        if (in < 0){
                    316:                signbit = -1;
                    317:                in *= -1;
                    318:        }
                    319:
                    320:        if (in >= SECS_PER_DAY ){
                    321:                unit = "days";
                    322:                in /= SECS_PER_DAY;
                    323:        } else if (in >= SECS_PER_HOUR ){
                    324:                unit = "hr";
                    325:                in /= SECS_PER_HOUR;
                    326:        } else if (in >= SECS_PER_MIN ){
                    327:                unit = "min";
                    328:                in /= SECS_PER_MIN;
                    329:        } else if (in >= 1 ){
1.11      ckuethe   330:                unit = "s";
1.9       ckuethe   331:                /* in *= 1; */ /* no op */
1.10      ckuethe   332:        } else if (in == 0 ){ /* direct comparisons to floats are scary */
1.11      ckuethe   333:                unit = "s";
1.9       ckuethe   334:        } else if (in >= 1e-3 ){
1.11      ckuethe   335:                unit = "ms";
1.9       ckuethe   336:                in *= 1e3;
                    337:        } else if (in >= 1e-6 ){
1.11      ckuethe   338:                unit = "us";
1.9       ckuethe   339:                in *= 1e6;
                    340:        } else if (in >= 1e-9 ){
1.11      ckuethe   341:                unit = "ns";
1.9       ckuethe   342:                in *= 1e9;
                    343:        } else {
1.11      ckuethe   344:                unit = "ps";
1.9       ckuethe   345:                if (in < 1e-13)
                    346:                        tiny = 1;
                    347:                in *= 1e12;
                    348:        }
                    349:
                    350:        snprintf(outbuf, LEN,
1.14      canacar   351:            tiny ? "%s%f %s" : "%s%.3f %s",
1.9       ckuethe   352:            signbit == -1 ? "-" : "", in, unit);
                    353:
                    354:        return outbuf;
1.1       deanna    355: }