~ubuntu-branches/ubuntu/precise/python-psutil/precise

« back to all changes in this revision

Viewing changes to psutil/_psutil_osx.c

  • Committer: Charlie Smotherman
  • Date: 2011-12-05 06:55:50 UTC
  • mfrom: (2.1.8 sid)
  • Revision ID: cjsmo@cableone.net-20111205065550-lkhxf2gxwzm180w7
Tags: 0.4.0-1ubuntu1
* Merge with Debian unstable (LP: #900001).  Remaining Ubuntu changes:
  - Switch to dh_python2. (LP: #788514)
* New upstream release
* debian/python-psutil.examples
  - install examples
* debian/rules
  - don't compress .py files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * $Id: _psutil_osx.c 1121 2011-09-13 18:14:10Z g.rodola@gmail.com $
 
2
 * $Id: _psutil_osx.c 1193 2011-10-22 18:24:53Z g.rodola@gmail.com $
 
3
 *
 
4
 * Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved.
 
5
 * Use of this source code is governed by a BSD-style license that can be
 
6
 * found in the LICENSE file.
3
7
 *
4
8
 * OS X platform-specific module methods for _psutil_osx
5
9
 */
26
30
#include <mach/mach_traps.h>
27
31
#include <mach/shared_memory_server.h>
28
32
 
 
33
#include <CoreFoundation/CoreFoundation.h>
 
34
#include <IOKit/IOKitLib.h>
 
35
#include <IOKit/storage/IOBlockStorageDriver.h>
 
36
#include <IOKit/storage/IOMedia.h>
 
37
#include <IOKit/IOBSD.h>
 
38
 
29
39
#include "_psutil_osx.h"
30
40
#include "_psutil_common.h"
31
41
#include "arch/osx/process_info.h"
59
69
            Py_XDECREF(pid);
60
70
            proclist++;
61
71
        }
 
72
        free(orig_address);
62
73
    }
63
 
    free(orig_address);
64
74
    return retlist;
65
75
}
66
76
 
908
918
    struct proc_fdinfo *fdp_pointer;
909
919
    struct socket_fdinfo si;
910
920
 
911
 
 
912
921
    PyObject *retList = PyList_New(0);
913
922
    PyObject *tuple = NULL;
914
923
    PyObject *laddr = NULL;
915
924
    PyObject *raddr = NULL;
916
 
 
917
 
    if (! PyArg_ParseTuple(args, "l", &pid)) {
 
925
    PyObject *af_filter = NULL;
 
926
    PyObject *type_filter = NULL;
 
927
 
 
928
    if (! PyArg_ParseTuple(args, "lOO", &pid, &af_filter, &type_filter)) {
 
929
        return NULL;
 
930
    }
 
931
 
 
932
    if (!PySequence_Check(af_filter) || !PySequence_Check(type_filter)) {
 
933
        PyErr_SetString(PyExc_TypeError, "arg 2 or 3 is not a sequence");
918
934
        return NULL;
919
935
    }
920
936
 
972
988
            int fd, family, type, lport, rport;
973
989
            char lip[200], rip[200];
974
990
            char *state;
 
991
            int inseq;
975
992
 
976
993
            fd = (int)fdp_pointer->proc_fd;
977
994
            family = si.psi.soi_family;
978
995
            type = si.psi.soi_kind;
979
996
 
980
 
            if ((family != AF_INET) && (family != AF_INET6)) {
981
 
                continue;
982
 
            }
983
 
 
984
997
            if (type == 2)
985
998
                type = SOCK_STREAM;
986
999
            else if (type == 1)
988
1001
            else
989
1002
                continue;
990
1003
 
 
1004
            // apply filters
 
1005
            inseq = PySequence_Contains(af_filter, PyLong_FromLong((long)family));
 
1006
            if (inseq == 0)
 
1007
                continue;
 
1008
            inseq = PySequence_Contains(type_filter, PyLong_FromLong((long)type));
 
1009
            if (inseq == 0)
 
1010
                continue;
 
1011
 
991
1012
            if (errno != 0) {
992
 
                printf("errno 1 = %i\n", errno);
993
1013
                return PyErr_SetFromErrno(PyExc_OSError);
994
1014
            }
995
1015
 
996
 
 
997
1016
            if (family == AF_INET) {
998
1017
                inet_ntop(AF_INET,
999
1018
                          &si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_laddr.ina_46.i46a_addr4,
1059
1078
static PyObject*
1060
1079
get_network_io_counters(PyObject* self, PyObject* args)
1061
1080
{
 
1081
    PyObject* py_retdict = PyDict_New();
 
1082
    PyObject* py_ifc_info;
 
1083
 
1062
1084
    char *buf = NULL, *lim, *next;
1063
1085
    struct if_msghdr *ifm;
1064
1086
    int mib[6];
1065
1087
    size_t len;
1066
 
    PyObject* py_retlist = PyList_New(0);
1067
 
    PyObject* py_ifc_info;
1068
1088
 
1069
1089
    mib[0] = CTL_NET;          // networking subsystem
1070
1090
    mib[1] = PF_ROUTE;         // type of information
1074
1094
    mib[5] = 0;
1075
1095
 
1076
1096
    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
 
1097
        Py_DECREF(py_retdict);
1077
1098
        PyErr_SetFromErrno(0);
1078
1099
        return NULL;
1079
1100
    }
1084
1105
        if (buf) {
1085
1106
            free(buf);
1086
1107
        }
 
1108
        Py_DECREF(py_retdict);
1087
1109
        PyErr_SetFromErrno(0);
1088
1110
        return NULL;
1089
1111
    }
1102
1124
            strncpy(ifc_name, sdl->sdl_data, sdl->sdl_nlen);
1103
1125
            ifc_name[sdl->sdl_nlen] = 0;
1104
1126
 
1105
 
            py_ifc_info = Py_BuildValue("(sKKKK)",
1106
 
                                        ifc_name,
 
1127
            py_ifc_info = Py_BuildValue("(KKKK)",
1107
1128
                                        if2m->ifm_data.ifi_obytes,
1108
1129
                                        if2m->ifm_data.ifi_ibytes,
1109
1130
                                        if2m->ifm_data.ifi_opackets,
1110
1131
                                        if2m->ifm_data.ifi_ipackets);
1111
 
            PyList_Append(py_retlist, py_ifc_info);
 
1132
            PyDict_SetItemString(py_retdict, ifc_name, py_ifc_info);
1112
1133
            Py_XDECREF(py_ifc_info);
1113
1134
        }
1114
1135
        else {
1116
1137
        }
1117
1138
    }
1118
1139
 
1119
 
    return py_retlist;
 
1140
    free(buf);
 
1141
 
 
1142
    return py_retdict;
 
1143
}
 
1144
 
 
1145
 
 
1146
/*
 
1147
 * Return a Python dict of tuples for disk I/O information
 
1148
 */
 
1149
static PyObject*
 
1150
get_disk_io_counters(PyObject* self, PyObject* args)
 
1151
{
 
1152
    PyObject* py_retdict = PyDict_New();
 
1153
    PyObject* py_disk_info;
 
1154
 
 
1155
    CFDictionaryRef parent_dict;
 
1156
    CFDictionaryRef props_dict;
 
1157
    CFDictionaryRef stats_dict;
 
1158
    io_registry_entry_t parent;
 
1159
    io_registry_entry_t disk;
 
1160
    io_iterator_t disk_list;
 
1161
 
 
1162
    /* Get list of disks */
 
1163
    if (IOServiceGetMatchingServices(kIOMasterPortDefault,
 
1164
                                     IOServiceMatching(kIOMediaClass),
 
1165
                                     &disk_list) != kIOReturnSuccess) {
 
1166
        Py_DECREF(py_retdict);
 
1167
        PyErr_SetString(PyExc_RuntimeError, "Unable to get the list of disks.");
 
1168
        return NULL;
 
1169
    }
 
1170
 
 
1171
    /* Iterate over disks */
 
1172
    while ((disk = IOIteratorNext(disk_list)) != 0) {
 
1173
        parent_dict = NULL;
 
1174
        props_dict = NULL;
 
1175
        stats_dict = NULL;
 
1176
 
 
1177
        if (IORegistryEntryGetParentEntry(disk, kIOServicePlane, &parent) != kIOReturnSuccess) {
 
1178
            PyErr_SetString(PyExc_RuntimeError, "Unable to get the disk's parent.");
 
1179
            Py_DECREF(py_retdict);
 
1180
            IOObjectRelease(disk);
 
1181
            return NULL;
 
1182
        }
 
1183
 
 
1184
        if (IOObjectConformsTo(parent, "IOBlockStorageDriver")) {
 
1185
            if(IORegistryEntryCreateCFProperties(
 
1186
                                     disk,
 
1187
                                     (CFMutableDictionaryRef *) &parent_dict,
 
1188
                                     kCFAllocatorDefault,
 
1189
                                     kNilOptions) != kIOReturnSuccess)
 
1190
            {
 
1191
                PyErr_SetString(PyExc_RuntimeError,
 
1192
                                "Unable to get the parent's properties.");
 
1193
                Py_DECREF(py_retdict);
 
1194
                IOObjectRelease(disk);
 
1195
                IOObjectRelease(parent);
 
1196
                return NULL;
 
1197
            }
 
1198
 
 
1199
            if (IORegistryEntryCreateCFProperties(parent,
 
1200
                                          (CFMutableDictionaryRef *) &props_dict,
 
1201
                                          kCFAllocatorDefault,
 
1202
                                          kNilOptions) != kIOReturnSuccess)
 
1203
            {
 
1204
                PyErr_SetString(PyExc_RuntimeError,
 
1205
                                "Unable to get the disk properties.");
 
1206
                Py_DECREF(py_retdict);
 
1207
                IOObjectRelease(disk);
 
1208
                return NULL;
 
1209
            }
 
1210
 
 
1211
            const int kMaxDiskNameSize = 64;
 
1212
            CFStringRef disk_name_ref = (CFStringRef)CFDictionaryGetValue(
 
1213
                                                    parent_dict,
 
1214
                                                    CFSTR(kIOBSDNameKey));
 
1215
            char disk_name[kMaxDiskNameSize];
 
1216
 
 
1217
            CFStringGetCString(disk_name_ref,
 
1218
                               disk_name,
 
1219
                               kMaxDiskNameSize,
 
1220
                               CFStringGetSystemEncoding());
 
1221
 
 
1222
            stats_dict = (CFDictionaryRef)CFDictionaryGetValue(
 
1223
                                    props_dict,
 
1224
                                    CFSTR(kIOBlockStorageDriverStatisticsKey));
 
1225
 
 
1226
            if (stats_dict == NULL) {
 
1227
                PyErr_SetString(PyExc_RuntimeError, "Unable to get disk stats.");
 
1228
                Py_DECREF(py_retdict);
 
1229
                CFRelease(props_dict);
 
1230
                IOObjectRelease(disk);
 
1231
                IOObjectRelease(parent);
 
1232
                return NULL;
 
1233
            }
 
1234
 
 
1235
            CFNumberRef number;
 
1236
            int64_t reads, writes, read_bytes, write_bytes, read_time, write_time = 0;
 
1237
 
 
1238
            /* Get disk reads/writes */
 
1239
            if ((number = (CFNumberRef)CFDictionaryGetValue(
 
1240
                            stats_dict,
 
1241
                            CFSTR(kIOBlockStorageDriverStatisticsReadsKey))))
 
1242
            {
 
1243
                CFNumberGetValue(number, kCFNumberSInt64Type, &reads);
 
1244
            }
 
1245
            if ((number = (CFNumberRef)CFDictionaryGetValue(
 
1246
                            stats_dict,
 
1247
                            CFSTR(kIOBlockStorageDriverStatisticsWritesKey))))
 
1248
            {
 
1249
                CFNumberGetValue(number, kCFNumberSInt64Type, &writes);
 
1250
            }
 
1251
 
 
1252
            /* Get disk bytes read/written */
 
1253
            if ((number = (CFNumberRef)CFDictionaryGetValue(
 
1254
                        stats_dict,
 
1255
                        CFSTR(kIOBlockStorageDriverStatisticsBytesReadKey))))
 
1256
            {
 
1257
                CFNumberGetValue(number, kCFNumberSInt64Type, &read_bytes);
 
1258
            }
 
1259
            if ((number = (CFNumberRef)CFDictionaryGetValue(
 
1260
                stats_dict,
 
1261
                CFSTR(kIOBlockStorageDriverStatisticsBytesWrittenKey))))
 
1262
            {
 
1263
                CFNumberGetValue(number, kCFNumberSInt64Type, &write_bytes);
 
1264
            }
 
1265
 
 
1266
            /* Get disk time spent reading/writing (nanoseconds) */
 
1267
            if ((number = (CFNumberRef)CFDictionaryGetValue(
 
1268
                    stats_dict,
 
1269
                    CFSTR(kIOBlockStorageDriverStatisticsTotalReadTimeKey))))
 
1270
            {
 
1271
                CFNumberGetValue(number, kCFNumberSInt64Type, &read_time);
 
1272
            }
 
1273
            if ((number = (CFNumberRef)CFDictionaryGetValue(
 
1274
                    stats_dict,
 
1275
                    CFSTR(kIOBlockStorageDriverStatisticsTotalWriteTimeKey)))) {
 
1276
                CFNumberGetValue(number, kCFNumberSInt64Type, &write_time);
 
1277
            }
 
1278
 
 
1279
            // Read/Write time on OS X comes back in nanoseconds and in psutil
 
1280
            // we've standardized on milliseconds so do the conversion.
 
1281
            py_disk_info = Py_BuildValue("(KKKKKK)",
 
1282
                                         reads, writes,
 
1283
                                         read_bytes, write_bytes,
 
1284
                                         read_time / 1000, write_time / 1000);
 
1285
            PyDict_SetItemString(py_retdict, disk_name, py_disk_info);
 
1286
            Py_XDECREF(py_disk_info);
 
1287
 
 
1288
            CFRelease(parent_dict);
 
1289
            IOObjectRelease(parent);
 
1290
            CFRelease(props_dict);
 
1291
            IOObjectRelease(disk);
 
1292
        }
 
1293
    }
 
1294
 
 
1295
    IOObjectRelease (disk_list);
 
1296
 
 
1297
    return py_retdict;
1120
1298
}
1121
1299
 
1122
1300
 
1183
1361
         "Return a list of tuples including device, mount point and "
1184
1362
         "fs type for all partitions mounted on the system."},
1185
1363
     {"get_network_io_counters", get_network_io_counters, METH_VARARGS,
1186
 
         "Return a tuple of cumulative network I/O information."},
 
1364
         "Return dict of tuples of networks I/O information."},
 
1365
     {"get_disk_io_counters", get_disk_io_counters, METH_VARARGS,
 
1366
         "Return dict of tuples of disks I/O information."},
1187
1367
 
1188
1368
     {NULL, NULL, 0, NULL}
1189
1369
};
1259
1439
    return module;
1260
1440
#endif
1261
1441
}
1262
 
 
1263