Annotation of XNU/osfmk/kern/mach_factor.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
        !             3:  *
        !             4:  * @APPLE_LICENSE_HEADER_START@
        !             5:  * 
        !             6:  * The contents of this file constitute Original Code as defined in and
        !             7:  * are subject to the Apple Public Source License Version 1.1 (the
        !             8:  * "License").  You may not use this file except in compliance with the
        !             9:  * License.  Please obtain a copy of the License at
        !            10:  * http://www.apple.com/publicsource and read it before using this file.
        !            11:  * 
        !            12:  * This Original Code and all software distributed under the License are
        !            13:  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
        !            14:  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
        !            15:  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
        !            16:  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
        !            17:  * License for the specific language governing rights and limitations
        !            18:  * under the License.
        !            19:  * 
        !            20:  * @APPLE_LICENSE_HEADER_END@
        !            21:  */
        !            22: /*
        !            23:  * @OSF_COPYRIGHT@
        !            24:  */
        !            25: /* 
        !            26:  * Mach Operating System
        !            27:  * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
        !            28:  * All Rights Reserved.
        !            29:  * 
        !            30:  * Permission to use, copy, modify and distribute this software and its
        !            31:  * documentation is hereby granted, provided that both the copyright
        !            32:  * notice and this permission notice appear in all copies of the
        !            33:  * software, derivative works or modified versions, and any portions
        !            34:  * thereof, and that both notices appear in supporting documentation.
        !            35:  * 
        !            36:  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
        !            37:  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
        !            38:  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
        !            39:  * 
        !            40:  * Carnegie Mellon requests users of this software to return to
        !            41:  * 
        !            42:  *  Software Distribution Coordinator  or  [email protected]
        !            43:  *  School of Computer Science
        !            44:  *  Carnegie Mellon University
        !            45:  *  Pittsburgh PA 15213-3890
        !            46:  * 
        !            47:  * any improvements or extensions that they make and grant Carnegie Mellon
        !            48:  * the rights to redistribute these changes.
        !            49:  */
        !            50: /*
        !            51:  */
        !            52: /*
        !            53:  *     File:   kern/mach_factor.c
        !            54:  *     Author: Avadis Tevanian, Jr.
        !            55:  *     Date:   1986
        !            56:  *
        !            57:  *     Compute the Mach Factor.
        !            58:  */
        !            59: 
        !            60: #include <cpus.h>
        !            61: 
        !            62: #include <mach/machine.h>
        !            63: #include <mach/processor_info.h>
        !            64: #include <kern/sched.h>
        !            65: #include <kern/assert.h>
        !            66: #include <kern/processor.h>
        !            67: #include <kern/thread.h>
        !            68: #if    MACH_KERNEL
        !            69: #include <mach/kern_return.h>
        !            70: #include <mach/port.h>
        !            71: #endif /* MACH_KERNEL */
        !            72: 
        !            73: integer_t      avenrun[3] = {0, 0, 0};
        !            74: integer_t      mach_factor[3] = {0, 0, 0};
        !            75: 
        !            76: /*
        !            77:  * Values are scaled by LOAD_SCALE, defined in processor_info.h
        !            78:  */
        !            79: static long    fract[3] = {
        !            80:        800,                    /* (4.0/5.0) 5 second average */
        !            81:        966,                    /* (29.0/30.0) 30 second average */
        !            82:        983,                    /* (59.0/60.) 1 minute average */
        !            83: };
        !            84: 
        !            85: void
        !            86: compute_mach_factor(void)
        !            87: {
        !            88:        register processor_set_t        pset;
        !            89:        register processor_t            processor;
        !            90:        register int                            ncpus;
        !            91:        register int                            nthreads;
        !            92:        register long                           factor_now;
        !            93:        register long                           average_now;
        !            94:        register long                           load_now;
        !            95: 
        !            96:        mutex_lock(&all_psets_lock);
        !            97:        pset = (processor_set_t) queue_first(&all_psets);
        !            98:        while (!queue_end(&all_psets, (queue_entry_t)pset)) {
        !            99:            /*
        !           100:             *  If no processors, this pset is in suspended animation.
        !           101:             *  No load calculations are performed.
        !           102:             */
        !           103:            pset_lock(pset);
        !           104:            if ((ncpus = pset->processor_count) > 0) {
        !           105:                        /*
        !           106:                         *      Count number of threads.
        !           107:                         */
        !           108:                        nthreads = pset->runq.count - pset->runq.depress_count;
        !           109: #if    NCPUS > 1
        !           110:                        processor = (processor_t)queue_first(&pset->processors);
        !           111:                        while (!queue_end(&pset->processors, (queue_entry_t)processor)) {
        !           112:                                nthreads += processor->runq.count -
        !           113:                                                                        processor->runq.depress_count;
        !           114:                                processor = (processor_t)queue_next(&processor->processors);
        !           115:                }
        !           116: #endif
        !           117: 
        !           118:                        /*
        !           119:                         * account for threads on cpus.
        !           120:                         */
        !           121:                        nthreads += ncpus - pset->idle_count; 
        !           122: 
        !           123:                        /*
        !           124:                         *      The current thread (running this calculation)
        !           125:                         *      doesn't count; it's always in the default pset.
        !           126:                         */
        !           127:                        if (pset == &default_pset)
        !           128:                                nthreads -= 1;
        !           129: 
        !           130:                        if (nthreads >= ncpus) {
        !           131:                                factor_now = (ncpus * LOAD_SCALE) / (nthreads + 1);
        !           132:                                load_now = (nthreads << SCHED_SHIFT) / ncpus;
        !           133:                        }
        !           134:                        else {
        !           135:                                factor_now = (ncpus - nthreads) * LOAD_SCALE;
        !           136:                                load_now = SCHED_SCALE;
        !           137:                        }
        !           138: 
        !           139:                        /*
        !           140:                         *      Load average and mach factor calculations for
        !           141:                         *      those that ask about these things.
        !           142:                         */
        !           143: 
        !           144:                        average_now = (nthreads * LOAD_SCALE) / ncpus;
        !           145: 
        !           146:                        pset->mach_factor =     ((pset->mach_factor << 2) + factor_now) / 5;
        !           147:                        pset->load_average = ((pset->load_average << 2) + average_now) / 5;
        !           148: 
        !           149:                        /*
        !           150:                         *      And some ugly stuff to keep w happy.
        !           151:                         */
        !           152:                        if (pset == &default_pset) {
        !           153:                                register int i;
        !           154: 
        !           155:                                for (i = 0; i < 3; i++) {
        !           156:                                        mach_factor[i] = ((mach_factor[i] * fract[i]) +
        !           157:                                                (factor_now * (LOAD_SCALE - fract[i]))) / LOAD_SCALE;
        !           158: 
        !           159:                                        avenrun[i] = ((avenrun[i] * fract[i]) +
        !           160:                                                (average_now * (LOAD_SCALE - fract[i]))) / LOAD_SCALE;
        !           161:                                }
        !           162:                        }
        !           163: 
        !           164:                        /*
        !           165:                         *      sched_load is the only thing used by scheduler.
        !           166:                         *      It is always at least 1 (i.e. SCHED_SCALE).
        !           167:                         */
        !           168:                        pset->sched_load = (pset->sched_load + load_now) >> 1;
        !           169:            }
        !           170: 
        !           171:            pset_unlock(pset);
        !           172:            pset = (processor_set_t) queue_next(&pset->all_psets);
        !           173:        }
        !           174: 
        !           175:        mutex_unlock(&all_psets_lock);
        !           176: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.