From 57cab491e7f6d6f2e4d1854b8d1ee959c064b1bb Mon Sep 17 00:00:00 2001 From: Apple Date: Wed, 12 Dec 2012 23:34:33 +0000 Subject: [PATCH] shell_cmds-175.tar.gz --- hexdump/display.c | 5 + hexdump/parse.c | 5 + killall/killall.c | 4 +- shell_cmds.xcodeproj/project.pbxproj | 109 ++++++++++++- systime/systime.1 | 53 ++++++ systime/systime.c | 236 +++++++++++++++++++++++++++ 6 files changed, 402 insertions(+), 10 deletions(-) create mode 100644 systime/systime.1 create mode 100644 systime/systime.c diff --git a/hexdump/display.c b/hexdump/display.c index c0070a1..978a50c 100644 --- a/hexdump/display.c +++ b/hexdump/display.c @@ -270,7 +270,12 @@ get(void) * XXX bcmp() is not quite right in the presence * of multibyte characters. */ +#ifdef __APPLE__ + /* 5650060 */ + if (!need && vflag != ALL && +#else if (vflag != ALL && +#endif valid_save && bcmp(curp, savp, nread) == 0) { if (vflag != DUP) diff --git a/hexdump/parse.c b/hexdump/parse.c index 4f81cb7..5445f18 100644 --- a/hexdump/parse.c +++ b/hexdump/parse.c @@ -295,6 +295,11 @@ isint: cs[2] = '\0'; case 2: pr->bcnt = 2; break; +#ifdef __APPLE__ + case 8: + pr->bcnt = 8; + break; +#endif /* __APPLE__ */ default: p1[1] = '\0'; badcnt((char *)p1); diff --git a/killall/killall.c b/killall/killall.c index 6f23a70..cf2aabb 100644 --- a/killall/killall.c +++ b/killall/killall.c @@ -360,7 +360,7 @@ main(int ac, char **av) for (i = 0; i < nprocs; i++) { #ifdef __APPLE__ - if ((procs[i].kp_proc.p_stat & SZOMB) == SZOMB && !zflag) + if (procs[i].kp_proc.p_stat == SZOMB && !zflag) continue; thispid = procs[i].kp_proc.p_pid; @@ -424,7 +424,7 @@ main(int ac, char **av) thistdev = procs[i].kp_eproc.e_tdev; #else /* !__APPLE__ */ - if ((procs[i].ki_stat & SZOMB) == SZOMB && !zflag) + if (procs[i].ki_stat == SZOMB && !zflag) continue; thispid = procs[i].ki_pid; strncpy(thiscmd, procs[i].ki_comm, MAXCOMLEN); diff --git a/shell_cmds.xcodeproj/project.pbxproj b/shell_cmds.xcodeproj/project.pbxproj index 8bbe58d..5f0eff8 100644 --- a/shell_cmds.xcodeproj/project.pbxproj +++ b/shell_cmds.xcodeproj/project.pbxproj @@ -14,6 +14,7 @@ FCE30EB814B531EC00CC0294 /* ShellScript */, ); dependencies = ( + C6868583154725A30025D623 /* PBXTargetDependency */, FCBA150114A144F700AA698B /* PBXTargetDependency */, FCBA150314A144F700AA698B /* PBXTargetDependency */, FCBA150514A144F700AA698B /* PBXTargetDependency */, @@ -73,6 +74,7 @@ FCE30F4D14B619C900CC0294 /* Run Script */, ); dependencies = ( + C6868581154725990025D623 /* PBXTargetDependency */, FCE30F4F14B619E600CC0294 /* PBXTargetDependency */, FCE30F5114B619E600CC0294 /* PBXTargetDependency */, FCE30F5314B619E600CC0294 /* PBXTargetDependency */, @@ -124,6 +126,8 @@ /* End PBXAggregateTarget section */ /* Begin PBXBuildFile section */ + C686857A154725700025D623 /* systime.c in Sources */ = {isa = PBXBuildFile; fileRef = C6868579154725700025D623 /* systime.c */; }; + C686857C154725700025D623 /* systime.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = C686857B154725700025D623 /* systime.1 */; }; FC2B5BFB14B3CCC600ECF511 /* uname.c in Sources */ = {isa = PBXBuildFile; fileRef = FCBA140914A141A300AA698B /* uname.c */; }; FC2B5BFD14B3CCD200ECF511 /* uname.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = FCBA140814A141A300AA698B /* uname.1 */; }; FC2B5BFE14B3CCD700ECF511 /* true.c in Sources */ = {isa = PBXBuildFile; fileRef = FCBA140414A141A300AA698B /* true.c */; }; @@ -252,6 +256,20 @@ /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ + C6868580154725990025D623 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = FC80BF5714A05A2F00C6F7F5 /* Project object */; + proxyType = 1; + remoteGlobalIDString = C6868575154725700025D623; + remoteInfo = systime; + }; + C6868582154725A30025D623 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = FC80BF5714A05A2F00C6F7F5 /* Project object */; + proxyType = 1; + remoteGlobalIDString = C6868575154725700025D623; + remoteInfo = systime; + }; FC5D63A114B9864400123E48 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = FC80BF5714A05A2F00C6F7F5 /* Project object */; @@ -899,6 +917,16 @@ /* End PBXContainerItemProxy section */ /* Begin PBXCopyFilesBuildPhase section */ + C6868574154725700025D623 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/local/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + C686857C154725700025D623 /* systime.1 in CopyFiles */, + ); + runOnlyForDeploymentPostprocessing = 1; + }; FC5D638614B9819E00123E48 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -1417,6 +1445,9 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + C6868576154725700025D623 /* systime */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = systime; sourceTree = BUILT_PRODUCTS_DIR; }; + C6868579154725700025D623 /* systime.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = systime.c; sourceTree = ""; }; + C686857B154725700025D623 /* systime.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = systime.1; sourceTree = ""; }; FC5D636814B9808E00123E48 /* conv.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = conv.c; sourceTree = ""; }; FC5D636914B9808E00123E48 /* display.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = display.c; sourceTree = ""; }; FC5D636A14B9808E00123E48 /* hexdump.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = hexdump.1; sourceTree = ""; }; @@ -1492,13 +1523,11 @@ FCBA13A914A141A300AA698B /* locate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = locate.h; sourceTree = ""; }; FCBA13AA14A141A300AA698B /* locate.rc */ = {isa = PBXFileReference; lastKnownFileType = text; path = locate.rc; sourceTree = ""; }; FCBA13AB14A141A300AA698B /* locate.updatedb.8 */ = {isa = PBXFileReference; lastKnownFileType = text; path = locate.updatedb.8; sourceTree = ""; }; - FCBA13AC14A141A300AA698B /* Makefile */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = ""; }; FCBA13AD14A141A300AA698B /* mklocatedb.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = mklocatedb.sh; sourceTree = ""; }; FCBA13AE14A141A300AA698B /* pathnames.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = pathnames.h; sourceTree = ""; }; FCBA13AF14A141A300AA698B /* updatedb.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = updatedb.sh; sourceTree = ""; }; FCBA13B014A141A300AA698B /* util.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = util.c; sourceTree = ""; }; FCBA13B114A141A300AA698B /* locate.code.8 */ = {isa = PBXFileReference; lastKnownFileType = text; path = locate.code.8; sourceTree = ""; }; - FCBA13B214A141A300AA698B /* Makefile */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = ""; }; FCBA13B414A141A300AA698B /* logname.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = logname.1; sourceTree = ""; }; FCBA13B514A141A300AA698B /* logname.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = logname.c; sourceTree = ""; }; FCBA13C014A141A300AA698B /* mktemp.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = mktemp.1; sourceTree = ""; }; @@ -1525,7 +1554,6 @@ FCBA13E914A141A300AA698B /* shlock.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = shlock.c; sourceTree = ""; }; FCBA13EC14A141A300AA698B /* sleep.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = sleep.1; sourceTree = ""; }; FCBA13ED14A141A300AA698B /* sleep.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = sleep.c; sourceTree = ""; }; - FCBA13EF14A141A300AA698B /* Makefile */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = ""; }; FCBA13F014A141A300AA698B /* su.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = su.1; sourceTree = ""; }; FCBA13F114A141A300AA698B /* su.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = su.c; sourceTree = ""; }; FCBA13F214A141A300AA698B /* su.pam */ = {isa = PBXFileReference; lastKnownFileType = text; path = su.pam; sourceTree = ""; }; @@ -1545,7 +1573,6 @@ FCBA140D14A141A300AA698B /* users.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = users.c; sourceTree = ""; }; FCBA140F14A141A300AA698B /* extern.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = extern.h; sourceTree = ""; }; FCBA141014A141A300AA698B /* fmt.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = fmt.c; sourceTree = ""; }; - FCBA141114A141A300AA698B /* Makefile */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = ""; }; FCBA141214A141A300AA698B /* pr_time.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = pr_time.c; sourceTree = ""; }; FCBA141314A141A300AA698B /* proc_compare.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = proc_compare.c; sourceTree = ""; }; FCBA141414A141A300AA698B /* uptime.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = uptime.1; sourceTree = ""; }; @@ -1616,6 +1643,13 @@ /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ + C6868573154725700025D623 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; FC5D638514B9819E00123E48 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -1956,6 +1990,15 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + C6868578154725700025D623 /* systime */ = { + isa = PBXGroup; + children = ( + C6868579154725700025D623 /* systime.c */, + C686857B154725700025D623 /* systime.1 */, + ); + path = systime; + sourceTree = ""; + }; FC5D636714B9808E00123E48 /* hexdump */ = { isa = PBXGroup; children = ( @@ -2018,6 +2061,7 @@ FCBA13E614A141A300AA698B /* shlock */, FCBA13EA14A141A300AA698B /* sleep */, FCBA13EE14A141A300AA698B /* su */, + C6868578154725700025D623 /* systime */, FCBA13F314A141A300AA698B /* tee */, FCBA13F714A141A300AA698B /* test */, FCBA13FD14A141A300AA698B /* time */, @@ -2086,6 +2130,7 @@ FCE30EDE14B536C900CC0294 /* locate.code */, FC5D638B14B9819E00123E48 /* what */, FC5D639714B9822D00123E48 /* hexdump */, + C6868576154725700025D623 /* systime */, ); name = Products; sourceTree = ""; @@ -2274,7 +2319,6 @@ FCBA139D14A141A300AA698B /* bigram */, FCBA13A014A141A300AA698B /* code */, FCBA13A314A141A300AA698B /* locate */, - FCBA13B214A141A300AA698B /* Makefile */, ); path = locate; sourceTree = ""; @@ -2307,7 +2351,6 @@ FCBA13A914A141A300AA698B /* locate.h */, FCBA13AA14A141A300AA698B /* locate.rc */, FCBA13AB14A141A300AA698B /* locate.updatedb.8 */, - FCBA13AC14A141A300AA698B /* Makefile */, FCBA13AD14A141A300AA698B /* mklocatedb.sh */, FCBA13AE14A141A300AA698B /* pathnames.h */, FCBA13AF14A141A300AA698B /* updatedb.sh */, @@ -2436,7 +2479,6 @@ FCBA13EE14A141A300AA698B /* su */ = { isa = PBXGroup; children = ( - FCBA13EF14A141A300AA698B /* Makefile */, FCBA13F014A141A300AA698B /* su.1 */, FCBA13F114A141A300AA698B /* su.c */, FCBA13F214A141A300AA698B /* su.pam */, @@ -2506,7 +2548,6 @@ children = ( FCBA140F14A141A300AA698B /* extern.h */, FCBA141014A141A300AA698B /* fmt.c */, - FCBA141114A141A300AA698B /* Makefile */, FCBA141214A141A300AA698B /* pr_time.c */, FCBA141314A141A300AA698B /* proc_compare.c */, FCBA141414A141A300AA698B /* uptime.1 */, @@ -2569,6 +2610,23 @@ /* End PBXGroup section */ /* Begin PBXNativeTarget section */ + C6868575154725700025D623 /* systime */ = { + isa = PBXNativeTarget; + buildConfigurationList = C686857E154725700025D623 /* Build configuration list for PBXNativeTarget "systime" */; + buildPhases = ( + C6868572154725700025D623 /* Sources */, + C6868573154725700025D623 /* Frameworks */, + C6868574154725700025D623 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = systime; + productName = systime; + productReference = C6868576154725700025D623 /* systime */; + productType = "com.apple.product-type.tool"; + }; FC5D637F14B9819E00123E48 /* what */ = { isa = PBXNativeTarget; buildConfigurationList = FC5D638914B9819E00123E48 /* Build configuration list for PBXNativeTarget "what" */; @@ -3446,6 +3504,7 @@ FCBA15C614A145A200AA698B /* shlock */, FCBA15CE14A145A500AA698B /* sleep */, FCBA15D614A145A700AA698B /* su */, + C6868575154725700025D623 /* systime */, FCBA15DE14A145A900AA698B /* tee */, FCBA15E614A145AE00AA698B /* test */, FCBA15EE14A145B000AA698B /* time */, @@ -3496,6 +3555,14 @@ /* End PBXShellScriptBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ + C6868572154725700025D623 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + C686857A154725700025D623 /* systime.c in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; FC5D638014B9819E00123E48 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -3903,6 +3970,16 @@ /* End PBXSourcesBuildPhase section */ /* Begin PBXTargetDependency section */ + C6868581154725990025D623 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = C6868575154725700025D623 /* systime */; + targetProxy = C6868580154725990025D623 /* PBXContainerItemProxy */; + }; + C6868583154725A30025D623 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = C6868575154725700025D623 /* systime */; + targetProxy = C6868582154725A30025D623 /* PBXContainerItemProxy */; + }; FC5D63A214B9864400123E48 /* PBXTargetDependency */ = { isa = PBXTargetDependency; target = FC5D638F14B9822D00123E48 /* hexdump */; @@ -4366,6 +4443,14 @@ /* End PBXTargetDependency section */ /* Begin XCBuildConfiguration section */ + C686857D154725700025D623 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + INSTALL_PATH = /usr/local/bin; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; FC5D638A14B9819E00123E48 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { @@ -4775,6 +4860,14 @@ /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ + C686857E154725700025D623 /* Build configuration list for PBXNativeTarget "systime" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + C686857D154725700025D623 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; FC5D638914B9819E00123E48 /* Build configuration list for PBXNativeTarget "what" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/systime/systime.1 b/systime/systime.1 new file mode 100644 index 0000000..43fb81f --- /dev/null +++ b/systime/systime.1 @@ -0,0 +1,53 @@ +.Dd April 24, 2012 +.Dt systime 1 +.Os Darwin +.Sh NAME +.Nm systime +.Nd gather system-wide time during program execution +.Sh SYNOPSIS +.Nm +.Op Fl P +.Ar utility Op Ar argument ... +.Nm +.Fl p +.Nm +.Op Fl P +.Fl u Ar user +.Fl s Ar sys +.Fl i Ar idle +.Sh DESCRIPTION +.Nm +is similar to +.Xr time 1 +in that it allows you to execute a utility program. However after execution +completes, it reports the system-wide time that was spent +during that time period, instead of just the portion directly caused +by the utility. This can be used to gather information about CPU +utilization by kernel threads (AIO, networking) and interrupt routines +on behalf of a program, but not directly attributed to it. +.Pp +The +.Nm +program can also be used to snapshot counters with +.Fl p +and then later used with +.Fl u Fl s Fl i +to calculate time spent during a time interval that doesn't correlate +to a specific command execution. For example: +.Pp +.Bd -ragged -offset indent +$ eval `systime -p` +.Pp +$ ... time passes ... +.Pp +$ systime -u $systime_user -s $systime_sys -i $systime_idle +.Pp +18.79 real 2.03 user 1.04 sys +.Ed +.Pp +.Fl P +can be used to print time spent as a percentage of overall CPU capacity +of the system (capped at 100%) +.Sh SEE ALSO +.Xr time 1 , +.Xr top 1 diff --git a/systime/systime.c b/systime/systime.c new file mode 100644 index 0000000..746a8bd --- /dev/null +++ b/systime/systime.c @@ -0,0 +1,236 @@ +/* + * Copyright (c) 2012 Apple Inc. All rights reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +static void usage(void); +static void do_print(void); +static void do_difftime(bool usepercent, uint64_t olduser, uint64_t oldsystem, uint64_t oldidle); +static kern_return_t get_processor_time(uint64_t *user, uint64_t *sys, uint64_t *idle); +static kern_return_t get_processor_count(int *ncpu); + +int +main(int argc, char *argv[]) +{ + int ch; + const char *optu = NULL; + const char *opts = NULL; + const char *opti = NULL; + int pid; + int status; + uint64_t olduser, oldsystem, oldidle; + kern_return_t kret; + bool usepercent = false; + + while ((ch = getopt(argc, argv, "Ppu:s:i:")) != -1) { + switch (ch) { + case 'P': + usepercent = true; + break; + case 'p': + do_print(); + exit(0); + break; + case 'u': + optu = optarg; + break; + case 's': + opts = optarg; + break; + case 'i': + opti = optarg; + break; + case '?': + default: + usage(); + } + } + + if (optu || opts || opti) { + char *endstr; + + if (!optu) + usage(); + olduser = strtoull(optu, &endstr, 0); + if (optu[0] == '\0' || endstr[0] != '\0') + usage(); + + if (!opts) + usage(); + oldsystem = strtoull(opts, &endstr, 0); + if (opts[0] == '\0' || endstr[0] != '\0') + usage(); + + if (!opti) + usage(); + oldidle = strtoull(opti, &endstr, 0); + if (opti[0] == '\0' || endstr[0] != '\0') + usage(); + + do_difftime(usepercent, olduser, oldsystem, oldidle); + exit(0); + } + + argc -= optind; + argv += optind; + + if (argc == 0) + usage(); + + kret = get_processor_time(&olduser, &oldsystem, &oldidle); + if (kret) + errx(1, "Error getting processor time: %s (%d)", mach_error_string(kret), kret); + + switch(pid = vfork()) { + case -1: /* error */ + perror("time"); + exit(1); + /* NOTREACHED */ + case 0: /* child */ + execvp(*argv, argv); + perror(*argv); + _exit((errno == ENOENT) ? 127 : 126); + /* NOTREACHED */ + } + + /* parent */ + (void)signal(SIGINT, SIG_IGN); + (void)signal(SIGQUIT, SIG_IGN); + while (wait(&status) != pid); + + do_difftime(usepercent, olduser, oldsystem, oldidle); + + exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE); + + return 0; +} + +static void +usage(void) +{ + fprintf(stderr, "usage: systime [-P] utility [argument ...]\n" + " systime -p\n" + " systime [-P] -u user -s sys -i idle\n"); + exit(1); +} + +static void +do_print(void) +{ + uint64_t user, system, idle; + kern_return_t kret; + + kret = get_processor_time(&user, &system, &idle); + if (kret) + errx(1, "Error getting processor time: %s (%d)", mach_error_string(kret), kret); + + printf("systime_user=%llu\n", user); + printf("systime_sys=%llu\n", system); + printf("systime_idle=%llu\n", idle); +} + +static void +do_difftime(bool usepercent, uint64_t olduser, uint64_t oldsystem, uint64_t oldidle) +{ + uint64_t user, system, idle; + uint64_t userelapsed, systemelapsed, idleelapsed, totalelapsed; + kern_return_t kret; + + kret = get_processor_time(&user, &system, &idle); + if (kret) + errx(1, "Error getting processor time: %s (%d)", mach_error_string(kret), kret); + + userelapsed = user - olduser; + systemelapsed = system - oldsystem; + idleelapsed = idle - oldidle; + totalelapsed = userelapsed + systemelapsed + idleelapsed; + + if (usepercent) { + fprintf(stderr, "%1.02f%% user %1.02f%% sys %1.02f%% idle\n", + ((double)userelapsed * 100)/totalelapsed, + ((double)systemelapsed * 100)/totalelapsed, + ((double)idleelapsed * 100)/totalelapsed); + } else { + int ncpu; + + kret = get_processor_count(&ncpu); + if (kret) + errx(1, "Error getting processor count: %s (%d)", mach_error_string(kret), kret); + + fprintf(stderr, "%1.02f real %1.02f user %1.02f sys\n", + ((double)totalelapsed) / 1000 /* ms per sec */ / ncpu, + ((double)userelapsed) / 1000, + ((double)systemelapsed) / 1000); + } +} + +static kern_return_t +get_processor_time(uint64_t *user, uint64_t *sys, uint64_t *idle) +{ + host_name_port_t host; + kern_return_t kret; + host_cpu_load_info_data_t host_load; + mach_msg_type_number_t count; + + host = mach_host_self(); + + count = HOST_CPU_LOAD_INFO_COUNT; + + kret = host_statistics(host, HOST_CPU_LOAD_INFO, (host_info_t)&host_load, &count); + if (kret) + return kret; + + *user = ((uint64_t)host_load.cpu_ticks[CPU_STATE_USER]) * 10 /* ms per tick */; + *sys = ((uint64_t)host_load.cpu_ticks[CPU_STATE_SYSTEM]) * 10; + *idle = ((uint64_t)host_load.cpu_ticks[CPU_STATE_IDLE]) * 10; + + return KERN_SUCCESS; +} + +static kern_return_t +get_processor_count(int *ncpu) +{ + host_name_port_t host; + kern_return_t kret; + host_basic_info_data_t hi; + mach_msg_type_number_t count; + + host = mach_host_self(); + + count = HOST_BASIC_INFO_COUNT; + + kret = host_info(host, HOST_BASIC_INFO, (host_info_t)&hi, &count); + if (kret) + return kret; + + *ncpu = hi.avail_cpus; + + return KERN_SUCCESS; +} -- 2.45.2