kern_acct.c revision 252415
1218822Sdim/*- 238889Sjdp * Copyright (c) 1982, 1986, 1989, 1993 3218822Sdim * The Regents of the University of California. All rights reserved. 4218822Sdim * (c) UNIX System Laboratories, Inc. 5218822Sdim * Copyright (c) 2005 Robert N. M. Watson 6218822Sdim * All rights reserved. 7218822Sdim * 838889Sjdp * All or some portions of this file are derived from material licensed 9218822Sdim * to the University of California by American Telephone and Telegraph 10218822Sdim * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11218822Sdim * the permission of UNIX System Laboratories, Inc. 12218822Sdim * 1338889Sjdp * Redistribution and use in source and binary forms, with or without 14218822Sdim * modification, are permitted provided that the following conditions 15218822Sdim * are met: 16218822Sdim * 1. Redistributions of source code must retain the above copyright 17218822Sdim * notice, this list of conditions and the following disclaimer. 18218822Sdim * 2. Redistributions in binary form must reproduce the above copyright 1938889Sjdp * notice, this list of conditions and the following disclaimer in the 20218822Sdim * documentation and/or other materials provided with the distribution. 21218822Sdim * 4. Neither the name of the University nor the names of its contributors 22218822Sdim * may be used to endorse or promote products derived from this software 23218822Sdim * without specific prior written permission. 24218822Sdim * 25218822Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26218822Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27218822Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28218822Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29218822Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30218822Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31218822Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32218822Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33218822Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34218822Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35218822Sdim * SUCH DAMAGE. 36218822Sdim * 37218822Sdim * Copyright (c) 1994 Christopher G. Demetriou 38218822Sdim * 39218822Sdim * Redistribution and use in source and binary forms, with or without 40218822Sdim * modification, are permitted provided that the following conditions 41218822Sdim * are met: 42218822Sdim * 1. Redistributions of source code must retain the above copyright 43218822Sdim * notice, this list of conditions and the following disclaimer. 44218822Sdim * 2. Redistributions in binary form must reproduce the above copyright 45218822Sdim * notice, this list of conditions and the following disclaimer in the 46218822Sdim * documentation and/or other materials provided with the distribution. 47218822Sdim * 3. All advertising materials mentioning features or use of this software 48218822Sdim * must display the following acknowledgement: 49218822Sdim * This product includes software developed by the University of 50218822Sdim * California, Berkeley and its contributors. 51218822Sdim * 4. Neither the name of the University nor the names of its contributors 52218822Sdim * may be used to endorse or promote products derived from this software 53218822Sdim * without specific prior written permission. 54218822Sdim * 55218822Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 56218822Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57218822Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58218822Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 59218822Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 60218822Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 61218822Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 62218822Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 63218822Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 64218822Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65218822Sdim * SUCH DAMAGE. 66218822Sdim * 67218822Sdim * @(#)kern_acct.c 8.1 (Berkeley) 6/14/93 68218822Sdim */ 69218822Sdim 70218822Sdim#include <sys/cdefs.h> 71218822Sdim__FBSDID("$FreeBSD: head/sys/kern/kern_acct.c 252415 2013-06-30 13:17:37Z mjg $"); 72218822Sdim 73218822Sdim#include <sys/param.h> 74218822Sdim#include <sys/systm.h> 75218822Sdim#include <sys/acct.h> 76218822Sdim#include <sys/fcntl.h> 77218822Sdim#include <sys/kernel.h> 78218822Sdim#include <sys/kthread.h> 79218822Sdim#include <sys/limits.h> 80218822Sdim#include <sys/lock.h> 81218822Sdim#include <sys/mount.h> 82218822Sdim#include <sys/mutex.h> 83218822Sdim#include <sys/namei.h> 8477298Sobrien#include <sys/priv.h> 8577298Sobrien#include <sys/proc.h> 86218822Sdim#include <sys/resourcevar.h> 87218822Sdim#include <sys/sched.h> 88218822Sdim#include <sys/sx.h> 89218822Sdim#include <sys/sysctl.h> 90218822Sdim#include <sys/sysent.h> 91218822Sdim#include <sys/syslog.h> 92218822Sdim#include <sys/sysproto.h> 93218822Sdim#include <sys/tty.h> 94218822Sdim#include <sys/vnode.h> 95218822Sdim 96218822Sdim#include <security/mac/mac_framework.h> 97218822Sdim 98218822Sdim/* 99218822Sdim * The routines implemented in this file are described in: 100218822Sdim * Leffler, et al.: The Design and Implementation of the 4.3BSD 101218822Sdim * UNIX Operating System (Addison Welley, 1989) 102218822Sdim * on pages 62-63. 103218822Sdim * On May 2007 the historic 3 bits base 8 exponent, 13 bit fraction 104218822Sdim * compt_t representation described in the above reference was replaced 105218822Sdim * with that of IEEE-754 floats. 106218822Sdim * 107218822Sdim * Arguably, to simplify accounting operations, this mechanism should 108218822Sdim * be replaced by one in which an accounting log file (similar to /dev/klog) 109218822Sdim * is read by a user process, etc. However, that has its own problems. 110218822Sdim */ 111218822Sdim 112218822Sdim/* Floating point definitions from <float.h>. */ 113218822Sdim#define FLT_MANT_DIG 24 /* p */ 114218822Sdim#define FLT_MAX_EXP 128 /* emax */ 115218822Sdim 116218822Sdim/* 117218822Sdim * Internal accounting functions. 118218822Sdim * The former's operation is described in Leffler, et al., and the latter 119218822Sdim * was provided by UCB with the 4.4BSD-Lite release 120218822Sdim */ 121218822Sdimstatic uint32_t encode_timeval(struct timeval); 122218822Sdimstatic uint32_t encode_long(long); 123218822Sdimstatic void acctwatch(void); 124218822Sdimstatic void acct_thread(void *); 125218822Sdimstatic int acct_disable(struct thread *, int); 126218822Sdim 127218822Sdim/* 128218822Sdim * Accounting vnode pointer, saved vnode pointer, and flags for each. 129218822Sdim * acct_sx protects against changes to the active vnode and credentials 130218822Sdim * while accounting records are being committed to disk. 131218822Sdim */ 132218822Sdimstatic int acct_configured; 133218822Sdimstatic int acct_suspended; 134218822Sdimstatic struct vnode *acct_vp; 135218822Sdimstatic struct ucred *acct_cred; 136218822Sdimstatic int acct_flags; 137218822Sdimstatic struct sx acct_sx; 138218822Sdim 139218822SdimSX_SYSINIT(acct, &acct_sx, "acct_sx"); 140218822Sdim 141218822Sdim/* 142218822Sdim * State of the accounting kthread. 143218822Sdim */ 144218822Sdimstatic int acct_state; 145218822Sdim 146218822Sdim#define ACCT_RUNNING 1 /* Accounting kthread is running. */ 147218822Sdim#define ACCT_EXITREQ 2 /* Accounting kthread should exit. */ 148218822Sdim 149218822Sdim/* 150218822Sdim * Values associated with enabling and disabling accounting 151218822Sdim */ 152218822Sdimstatic int acctsuspend = 2; /* stop accounting when < 2% free space left */ 153218822SdimSYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW, 154218822Sdim &acctsuspend, 0, "percentage of free disk space below which accounting stops"); 155218822Sdim 156218822Sdimstatic int acctresume = 4; /* resume when free space risen to > 4% */ 157218822SdimSYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW, 158218822Sdim &acctresume, 0, "percentage of free disk space above which accounting resumes"); 159218822Sdim 160218822Sdimstatic int acctchkfreq = 15; /* frequency (in seconds) to check space */ 161218822Sdim 162218822Sdimstatic int 163218822Sdimsysctl_acct_chkfreq(SYSCTL_HANDLER_ARGS) 164218822Sdim{ 165218822Sdim int error, value; 166218822Sdim 167218822Sdim /* Write out the old value. */ 168218822Sdim error = SYSCTL_OUT(req, &acctchkfreq, sizeof(int)); 169218822Sdim if (error || req->newptr == NULL) 170218822Sdim return (error); 171218822Sdim 172218822Sdim /* Read in and verify the new value. */ 173218822Sdim error = SYSCTL_IN(req, &value, sizeof(int)); 174218822Sdim if (error) 175218822Sdim return (error); 176218822Sdim if (value <= 0) 177218822Sdim return (EINVAL); 178218822Sdim acctchkfreq = value; 179218822Sdim return (0); 180218822Sdim} 181218822SdimSYSCTL_PROC(_kern, OID_AUTO, acct_chkfreq, CTLTYPE_INT|CTLFLAG_RW, 182218822Sdim &acctchkfreq, 0, sysctl_acct_chkfreq, "I", 183218822Sdim "frequency for checking the free space"); 184218822Sdim 185218822SdimSYSCTL_INT(_kern, OID_AUTO, acct_configured, CTLFLAG_RD, &acct_configured, 0, 186218822Sdim "Accounting configured or not"); 187218822Sdim 188218822SdimSYSCTL_INT(_kern, OID_AUTO, acct_suspended, CTLFLAG_RD, &acct_suspended, 0, 189218822Sdim "Accounting suspended or not"); 190218822Sdim 191218822Sdim/* 192218822Sdim * Accounting system call. Written based on the specification and previous 193218822Sdim * implementation done by Mark Tinguely. 194218822Sdim */ 195218822Sdimint 196218822Sdimsys_acct(struct thread *td, struct acct_args *uap) 197218822Sdim{ 198218822Sdim struct nameidata nd; 199218822Sdim int error, flags, replacing; 200218822Sdim 201218822Sdim error = priv_check(td, PRIV_ACCT); 202218822Sdim if (error) 203218822Sdim return (error); 204218822Sdim 205218822Sdim /* 206218822Sdim * If accounting is to be started to a file, open that file for 207218822Sdim * appending and make sure it's a 'normal'. 208218822Sdim */ 209218822Sdim if (uap->path != NULL) { 210218822Sdim NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, 211218822Sdim UIO_USERSPACE, uap->path, td); 212218822Sdim flags = FWRITE | O_APPEND; 213218822Sdim error = vn_open(&nd, &flags, 0, NULL); 214218822Sdim if (error) 215218822Sdim return (error); 216218822Sdim NDFREE(&nd, NDF_ONLY_PNBUF); 217218822Sdim#ifdef MAC 218218822Sdim error = mac_system_check_acct(td->td_ucred, nd.ni_vp); 219218822Sdim if (error) { 220218822Sdim VOP_UNLOCK(nd.ni_vp, 0); 221218822Sdim vn_close(nd.ni_vp, flags, td->td_ucred, td); 222218822Sdim return (error); 223218822Sdim } 224218822Sdim#endif 225218822Sdim VOP_UNLOCK(nd.ni_vp, 0); 226218822Sdim if (nd.ni_vp->v_type != VREG) { 227218822Sdim vn_close(nd.ni_vp, flags, td->td_ucred, td); 228218822Sdim return (EACCES); 229218822Sdim } 230218822Sdim#ifdef MAC 231218822Sdim } else { 232218822Sdim error = mac_system_check_acct(td->td_ucred, NULL); 233218822Sdim if (error) 234218822Sdim return (error); 235218822Sdim#endif 236218822Sdim } 237218822Sdim 238218822Sdim /* 239218822Sdim * Disallow concurrent access to the accounting vnode while we swap 240218822Sdim * it out, in order to prevent access after close. 241218822Sdim */ 242218822Sdim sx_xlock(&acct_sx); 24377298Sobrien 244218822Sdim /* 245218822Sdim * Don't log spurious disable/enable messages if we are 246218822Sdim * switching from one accounting file to another due to log 247218822Sdim * rotation. 248218822Sdim */ 24977298Sobrien replacing = (acct_vp != NULL && uap->path != NULL); 25077298Sobrien 251218822Sdim /* 252218822Sdim * If accounting was previously enabled, kill the old space-watcher, 253218822Sdim * close the file, and (if no new file was specified, leave). Reset 254218822Sdim * the suspended state regardless of whether accounting remains 255218822Sdim * enabled. 256218822Sdim */ 257218822Sdim acct_suspended = 0; 258218822Sdim if (acct_vp != NULL) 25977298Sobrien error = acct_disable(td, !replacing); 26077298Sobrien if (uap->path == NULL) { 261218822Sdim if (acct_state & ACCT_RUNNING) { 262218822Sdim acct_state |= ACCT_EXITREQ; 263218822Sdim wakeup(&acct_state); 264218822Sdim } 265218822Sdim sx_xunlock(&acct_sx); 266218822Sdim return (error); 267218822Sdim } 268218822Sdim 269218822Sdim /* 270218822Sdim * Save the new accounting file vnode, and schedule the new 271218822Sdim * free space watcher. 272218822Sdim */ 273218822Sdim acct_vp = nd.ni_vp; 274130561Sobrien acct_cred = crhold(td->td_ucred); 275218822Sdim acct_flags = flags; 276130561Sobrien if (acct_state & ACCT_RUNNING) 277218822Sdim acct_state &= ~ACCT_EXITREQ; 278218822Sdim else { 279218822Sdim /* 280218822Sdim * Try to start up an accounting kthread. We may start more 281218822Sdim * than one, but if so the extras will commit suicide as 282218822Sdim * soon as they start up. 283130561Sobrien */ 284218822Sdim error = kproc_create(acct_thread, NULL, NULL, 0, 0, 28538889Sjdp "accounting"); 286218822Sdim if (error) { 287218822Sdim (void) acct_disable(td, 0); 288218822Sdim sx_xunlock(&acct_sx); 289218822Sdim log(LOG_NOTICE, "Unable to start accounting thread\n"); 290218822Sdim return (error); 291218822Sdim } 292218822Sdim } 293218822Sdim acct_configured = 1; 294218822Sdim sx_xunlock(&acct_sx); 295218822Sdim if (!replacing) 296218822Sdim log(LOG_NOTICE, "Accounting enabled\n"); 297218822Sdim return (error); 298218822Sdim} 299218822Sdim 300218822Sdim/* 301218822Sdim * Disable currently in-progress accounting by closing the vnode, dropping 302218822Sdim * our reference to the credential, and clearing the vnode's flags. 303218822Sdim */ 304218822Sdimstatic int 305218822Sdimacct_disable(struct thread *td, int logging) 306218822Sdim{ 307218822Sdim int error; 308218822Sdim 309218822Sdim sx_assert(&acct_sx, SX_XLOCKED); 310218822Sdim error = vn_close(acct_vp, acct_flags, acct_cred, td); 311218822Sdim crfree(acct_cred); 312218822Sdim acct_configured = 0; 313218822Sdim acct_vp = NULL; 314218822Sdim acct_cred = NULL; 315218822Sdim acct_flags = 0; 316218822Sdim if (logging) 317218822Sdim log(LOG_NOTICE, "Accounting disabled\n"); 318218822Sdim return (error); 319218822Sdim} 320218822Sdim 321218822Sdim/* 322218822Sdim * Write out process accounting information, on process exit. 323218822Sdim * Data to be written out is specified in Leffler, et al. 324218822Sdim * and are enumerated below. (They're also noted in the system 325218822Sdim * "acct.h" header file.) 326218822Sdim */ 327218822Sdimint 32838889Sjdpacct_process(struct thread *td) 32938889Sjdp{ 330218822Sdim struct acctv2 acct; 331218822Sdim struct timeval ut, st, tmp; 332218822Sdim struct plimit *newlim, *oldlim; 333218822Sdim struct proc *p; 334218822Sdim struct rusage ru; 335218822Sdim int t, ret; 336218822Sdim 337218822Sdim /* 338218822Sdim * Lockless check of accounting condition before doing the hard 339218822Sdim * work. 340218822Sdim */ 341218822Sdim if (acct_vp == NULL || acct_suspended) 342218822Sdim return (0); 343218822Sdim 344218822Sdim sx_slock(&acct_sx); 345218822Sdim 346218822Sdim /* 347218822Sdim * If accounting isn't enabled, don't bother. Have to check again 348218822Sdim * once we own the lock in case we raced with disabling of accounting 349218822Sdim * by another thread. 350218822Sdim */ 351218822Sdim if (acct_vp == NULL || acct_suspended) { 352218822Sdim sx_sunlock(&acct_sx); 353218822Sdim return (0); 354218822Sdim } 355218822Sdim 356218822Sdim p = td->td_proc; 357218822Sdim 358218822Sdim /* 359218822Sdim * Get process accounting information. 360218822Sdim */ 361218822Sdim 362218822Sdim sx_slock(&proctree_lock); 363218822Sdim PROC_LOCK(p); 364218822Sdim 365218822Sdim /* (1) The terminal from which the process was started */ 366218822Sdim if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp) 367218822Sdim acct.ac_tty = tty_udev(p->p_pgrp->pg_session->s_ttyp); 368218822Sdim else 369218822Sdim acct.ac_tty = NODEV; 370218822Sdim sx_sunlock(&proctree_lock); 371218822Sdim 372218822Sdim /* (2) The name of the command that ran */ 373218822Sdim bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm); 374218822Sdim 375218822Sdim /* (3) The amount of user and system time that was used */ 376218822Sdim rufetchcalc(p, &ru, &ut, &st); 377218822Sdim acct.ac_utime = encode_timeval(ut); 378218822Sdim acct.ac_stime = encode_timeval(st); 379218822Sdim 38094536Sobrien /* (4) The elapsed time the command ran (and its starting time) */ 381218822Sdim tmp = boottime; 382218822Sdim timevaladd(&tmp, &p->p_stats->p_start); 383218822Sdim acct.ac_btime = tmp.tv_sec; 384218822Sdim microuptime(&tmp); 385218822Sdim timevalsub(&tmp, &p->p_stats->p_start); 386218822Sdim acct.ac_etime = encode_timeval(tmp); 387218822Sdim 388218822Sdim /* (5) The average amount of memory used */ 389218822Sdim tmp = ut; 39038889Sjdp timevaladd(&tmp, &st); 39138889Sjdp /* Convert tmp (i.e. u + s) into hz units to match ru_i*. */ 39238889Sjdp t = tmp.tv_sec * hz + tmp.tv_usec / tick; 393218822Sdim if (t) 394218822Sdim acct.ac_mem = encode_long((ru.ru_ixrss + ru.ru_idrss + 395218822Sdim + ru.ru_isrss) / t); 396218822Sdim else 397218822Sdim acct.ac_mem = 0; 398218822Sdim 399218822Sdim /* (6) The number of disk I/O operations done */ 400218822Sdim acct.ac_io = encode_long(ru.ru_inblock + ru.ru_oublock); 401218822Sdim 402218822Sdim /* (7) The UID and GID of the process */ 403218822Sdim acct.ac_uid = p->p_ucred->cr_ruid; 404218822Sdim acct.ac_gid = p->p_ucred->cr_rgid; 405218822Sdim 406218822Sdim /* (8) The boolean flags that tell how the process terminated, etc. */ 407218822Sdim acct.ac_flagx = p->p_acflag; 408218822Sdim PROC_UNLOCK(p); 409218822Sdim 410218822Sdim /* Setup ancillary structure fields. */ 411218822Sdim acct.ac_flagx |= ANVER; 412218822Sdim acct.ac_zero = 0; 413218822Sdim acct.ac_version = 2; 414218822Sdim acct.ac_len = acct.ac_len2 = sizeof(acct); 415218822Sdim 416218822Sdim /* 417218822Sdim * Eliminate any file size rlimit. 418218822Sdim */ 419218822Sdim newlim = lim_alloc(); 420218822Sdim PROC_LOCK(p); 421218822Sdim oldlim = p->p_limit; 422218822Sdim lim_copy(newlim, oldlim); 423218822Sdim newlim->pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 424218822Sdim p->p_limit = newlim; 425218822Sdim PROC_UNLOCK(p); 426218822Sdim lim_free(oldlim); 427218822Sdim 428218822Sdim /* 429218822Sdim * Write the accounting information to the file. 430218822Sdim */ 431218822Sdim ret = vn_rdwr(UIO_WRITE, acct_vp, (caddr_t)&acct, sizeof (acct), 432218822Sdim (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, acct_cred, NOCRED, 433218822Sdim NULL, td); 434218822Sdim sx_sunlock(&acct_sx); 435218822Sdim return (ret); 436218822Sdim} 437218822Sdim 438218822Sdim/* FLOAT_CONVERSION_START (Regression testing; don't remove this line.) */ 439218822Sdim 440218822Sdim/* Convert timevals and longs into IEEE-754 bit patterns. */ 441218822Sdim 442218822Sdim/* Mantissa mask (MSB is implied, so subtract 1). */ 443218822Sdim#define MANT_MASK ((1 << (FLT_MANT_DIG - 1)) - 1) 444218822Sdim 445218822Sdim/* 446218822Sdim * We calculate integer values to a precision of approximately 447218822Sdim * 28 bits. 448218822Sdim * This is high-enough precision to fill the 24 float bits 449218822Sdim * and low-enough to avoid overflowing the 32 int bits. 450218822Sdim */ 451218822Sdim#define CALC_BITS 28 452218822Sdim 453218822Sdim/* log_2(1000000). */ 454218822Sdim#define LOG2_1M 20 455218822Sdim 456218822Sdim/* 457218822Sdim * Convert the elements of a timeval into a 32-bit word holding 458218822Sdim * the bits of a IEEE-754 float. 459218822Sdim * The float value represents the timeval's value in microsecond units. 460218822Sdim */ 461218822Sdimstatic uint32_t 462218822Sdimencode_timeval(struct timeval tv) 463218822Sdim{ 464218822Sdim int log2_s; 465218822Sdim int val, exp; /* Unnormalized value and exponent */ 466218822Sdim int norm_exp; /* Normalized exponent */ 467218822Sdim int shift; 468218822Sdim 469218822Sdim /* 47038889Sjdp * First calculate value and exponent to about CALC_BITS precision. 471218822Sdim * Note that the following conditionals have been ordered so that 472218822Sdim * the most common cases appear first. 473218822Sdim */ 474218822Sdim if (tv.tv_sec == 0) { 475218822Sdim if (tv.tv_usec == 0) 476218822Sdim return (0); 477218822Sdim exp = 0; 478218822Sdim val = tv.tv_usec; 479218822Sdim } else { 480218822Sdim /* 481218822Sdim * Calculate the value to a precision of approximately 482218822Sdim * CALC_BITS. 483218822Sdim */ 484218822Sdim log2_s = fls(tv.tv_sec) - 1; 48538889Sjdp if (log2_s + LOG2_1M < CALC_BITS) { 486218822Sdim exp = 0; 487218822Sdim val = 1000000 * tv.tv_sec + tv.tv_usec; 488218822Sdim } else { 48938889Sjdp exp = log2_s + LOG2_1M - CALC_BITS; 490218822Sdim val = (unsigned int)(((uint64_t)1000000 * tv.tv_sec + 491218822Sdim tv.tv_usec) >> exp); 492218822Sdim } 493218822Sdim } 494218822Sdim /* Now normalize and pack the value into an IEEE-754 float. */ 495218822Sdim norm_exp = fls(val) - 1; 496218822Sdim shift = FLT_MANT_DIG - norm_exp - 1; 497218822Sdim#ifdef ACCT_DEBUG 498218822Sdim printf("val=%d exp=%d shift=%d log2(val)=%d\n", 499218822Sdim val, exp, shift, norm_exp); 500218822Sdim printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp, 501218822Sdim ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK)); 502218822Sdim#endif 503218822Sdim return (((FLT_MAX_EXP - 1 + exp + norm_exp) << (FLT_MANT_DIG - 1)) | 504218822Sdim ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK)); 505218822Sdim} 506218822Sdim 507218822Sdim/* 508218822Sdim * Convert a non-negative long value into the bit pattern of 509218822Sdim * an IEEE-754 float value. 510218822Sdim */ 511218822Sdimstatic uint32_t 512218822Sdimencode_long(long val) 513218822Sdim{ 514218822Sdim int norm_exp; /* Normalized exponent */ 515218822Sdim int shift; 516218822Sdim 517218822Sdim if (val == 0) 518218822Sdim return (0); 519218822Sdim if (val < 0) { 520218822Sdim log(LOG_NOTICE, 521218822Sdim "encode_long: negative value %ld in accounting record\n", 522218822Sdim val); 523218822Sdim val = LONG_MAX; 524218822Sdim } 525218822Sdim norm_exp = fls(val) - 1; 526218822Sdim shift = FLT_MANT_DIG - norm_exp - 1; 527218822Sdim#ifdef ACCT_DEBUG 528218822Sdim printf("val=%d shift=%d log2(val)=%d\n", 529218822Sdim val, shift, norm_exp); 530218822Sdim printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp, 531218822Sdim ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK)); 532218822Sdim#endif 533218822Sdim return (((FLT_MAX_EXP - 1 + norm_exp) << (FLT_MANT_DIG - 1)) | 534218822Sdim ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK)); 535218822Sdim} 536218822Sdim 537218822Sdim/* FLOAT_CONVERSION_END (Regression testing; don't remove this line.) */ 538218822Sdim 539218822Sdim/* 540218822Sdim * Periodically check the filesystem to see if accounting 541218822Sdim * should be turned on or off. Beware the case where the vnode 542218822Sdim * has been vgone()'d out from underneath us, e.g. when the file 543218822Sdim * system containing the accounting file has been forcibly unmounted. 544218822Sdim */ 545218822Sdim/* ARGSUSED */ 546218822Sdimstatic void 547218822Sdimacctwatch(void) 548218822Sdim{ 549218822Sdim struct statfs sb; 550218822Sdim 551218822Sdim sx_assert(&acct_sx, SX_XLOCKED); 552218822Sdim 553218822Sdim /* 554218822Sdim * If accounting was disabled before our kthread was scheduled, 555218822Sdim * then acct_vp might be NULL. If so, just ask our kthread to 556218822Sdim * exit and return. 557218822Sdim */ 558218822Sdim if (acct_vp == NULL) { 559218822Sdim acct_state |= ACCT_EXITREQ; 560218822Sdim return; 561218822Sdim } 562218822Sdim 563218822Sdim /* 564218822Sdim * If our vnode is no longer valid, tear it down and signal the 565218822Sdim * accounting thread to die. 566218822Sdim */ 567218822Sdim if (acct_vp->v_type == VBAD) { 568218822Sdim (void) acct_disable(NULL, 1); 569218822Sdim acct_state |= ACCT_EXITREQ; 570218822Sdim return; 571218822Sdim } 572218822Sdim 573218822Sdim /* 574218822Sdim * Stopping here is better than continuing, maybe it will be VBAD 575218822Sdim * next time around. 576218822Sdim */ 577218822Sdim if (VFS_STATFS(acct_vp->v_mount, &sb) < 0) 578218822Sdim return; 579218822Sdim if (acct_suspended) { 580218822Sdim if (sb.f_bavail > (int64_t)(acctresume * sb.f_blocks / 581218822Sdim 100)) { 582218822Sdim acct_suspended = 0; 583218822Sdim log(LOG_NOTICE, "Accounting resumed\n"); 584218822Sdim } 585218822Sdim } else { 586218822Sdim if (sb.f_bavail <= (int64_t)(acctsuspend * sb.f_blocks / 587218822Sdim 100)) { 588218822Sdim acct_suspended = 1; 589218822Sdim log(LOG_NOTICE, "Accounting suspended\n"); 590218822Sdim } 591218822Sdim } 592218822Sdim} 593218822Sdim 594218822Sdim/* 595218822Sdim * The main loop for the dedicated kernel thread that periodically calls 596218822Sdim * acctwatch(). 597218822Sdim */ 598218822Sdimstatic void 599218822Sdimacct_thread(void *dummy) 600218822Sdim{ 601218822Sdim u_char pri; 602218822Sdim 603218822Sdim /* This is a low-priority kernel thread. */ 604218822Sdim pri = PRI_MAX_KERN; 605218822Sdim thread_lock(curthread); 606218822Sdim sched_prio(curthread, pri); 607218822Sdim thread_unlock(curthread); 608218822Sdim 609218822Sdim /* If another accounting kthread is already running, just die. */ 610218822Sdim sx_xlock(&acct_sx); 611218822Sdim if (acct_state & ACCT_RUNNING) { 612218822Sdim sx_xunlock(&acct_sx); 613218822Sdim kproc_exit(0); 614218822Sdim } 615218822Sdim acct_state |= ACCT_RUNNING; 616218822Sdim 617218822Sdim /* Loop until we are asked to exit. */ 618218822Sdim while (!(acct_state & ACCT_EXITREQ)) { 619218822Sdim 620218822Sdim /* Perform our periodic checks. */ 621218822Sdim acctwatch(); 622218822Sdim 623218822Sdim /* 624218822Sdim * We check this flag again before sleeping since the 625218822Sdim * acctwatch() might have shut down accounting and asked us 626218822Sdim * to exit. 627218822Sdim */ 628218822Sdim if (!(acct_state & ACCT_EXITREQ)) { 629218822Sdim sx_sleep(&acct_state, &acct_sx, 0, "-", 630218822Sdim acctchkfreq * hz); 631218822Sdim } 632218822Sdim } 633218822Sdim 634218822Sdim /* 635218822Sdim * Acknowledge the exit request and shutdown. We clear both the 636218822Sdim * exit request and running flags. 637218822Sdim */ 638218822Sdim acct_state = 0; 639218822Sdim sx_xunlock(&acct_sx); 640218822Sdim kproc_exit(0); 641218822Sdim} 642218822Sdim