1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24cc clockfreq.c -o /tmp/clockfreq -Wall -Wno-four-char-constants -framework IOKit
25*/
26
27#include <ctype.h>
28#include <stdlib.h>
29#include <assert.h>
30#include <limits.h>
31#include <stdio.h>
32
33
34#include <IOKit/IOKitLib.h>
35
36
37mach_port_t	masterPort;
38
39
40void printInt32( CFDataRef data, CFStringRef key )
41{
42    UInt32	value;
43
44    printf("\"%s\" ", CFStringGetCStringPtr(key, kCFStringEncodingMacRoman));
45
46    if( data
47     && (CFDataGetLength(data) >= sizeof( value ))) {
48        value = *((UInt32 *)CFDataGetBytePtr(data));
49        printf("= %ld = %08lx\n", value, value );
50    } else
51	printf("not found\n");
52}
53
54void printDictInt32( CFDictionaryRef dict, CFStringRef key )
55{
56    printInt32( CFDictionaryGetValue(dict, key), key );
57}
58
59void printEntryInt32( io_registry_entry_t entry, CFStringRef key )
60{
61    CFDataRef data;
62
63    data = IORegistryEntryCreateCFProperty( entry, key,
64            kCFAllocatorDefault, kNilOptions );
65    printInt32( data, key );
66    if( data)
67        CFRelease(data);
68}
69
70void getClockFrequency( void )
71{
72    kern_return_t	kr;
73    io_registry_entry_t	root;
74    io_registry_entry_t	cpus;
75    io_registry_entry_t	cpu;
76    io_iterator_t	iter;
77    io_name_t		name;
78    CFDataRef		data;
79    CFDictionaryRef	properties;
80
81    assert( (
82    root = IORegistryEntryFromPath( masterPort,
83		kIODeviceTreePlane ":/" )
84    ));
85    assert( KERN_SUCCESS == (
86    kr = IORegistryEntryCreateCFProperties(root, &properties,
87                                        kCFAllocatorDefault, kNilOptions )
88    ));
89    data = CFDictionaryGetValue(properties, CFSTR("compatible"));
90
91    printf("machine ");
92    if( data)
93        printf(CFDataGetBytePtr(data));
94    printf("\n---------------------\n");
95    printDictInt32(properties, CFSTR("clock-frequency"));
96
97    CFRelease(properties);
98
99    // go looking for a cpu
100
101    if( (cpus = IORegistryEntryFromPath( masterPort,
102		kIODeviceTreePlane ":/cpus" ))) {
103        assert( KERN_SUCCESS == (
104	kr = IORegistryEntryGetChildIterator( cpus, kIODeviceTreePlane, &iter )
105        ));
106        IOObjectRelease( cpus );
107    } else {
108        assert( KERN_SUCCESS == (
109	kr = IORegistryEntryGetChildIterator( root, kIODeviceTreePlane, &iter )
110        ));
111    }
112
113    while( (cpu = IOIteratorNext( iter ))) {
114	if( (data = IORegistryEntryCreateCFProperty( cpu, CFSTR("device_type"),
115		kCFAllocatorDefault, kNilOptions ))
116	 && (0 == strcmp("cpu", CFDataGetBytePtr(data)))) {
117
118            printf("\nprocessor ");
119            if( KERN_SUCCESS == IORegistryEntryGetName( cpu, name))
120		printf(name);
121            printf("\n---------------------\n");
122
123
124            assert( KERN_SUCCESS == (
125            kr = IORegistryEntryCreateCFProperties(cpu, &properties,
126                                                kCFAllocatorDefault, kNilOptions )
127            ));
128
129	    printDictInt32(properties, CFSTR("clock-frequency"));
130	    printDictInt32(properties, CFSTR("bus-frequency"));
131	    printDictInt32(properties, CFSTR("timebase-frequency"));
132	    printDictInt32(properties, CFSTR("d-cache-size"));
133	    printDictInt32(properties, CFSTR("i-cache-size"));
134	    printDictInt32(properties, CFSTR("cpu-version"));
135
136            CFRelease(properties);
137	}
138	IOObjectRelease(cpu);
139    }
140
141    IOObjectRelease( iter );
142    IOObjectRelease( root );
143}
144
145int
146main(int argc, char **argv)
147{
148	kern_return_t		kr;
149
150	/*
151	 * Get master device port
152	 */
153	assert( KERN_SUCCESS == (
154	kr = IOMasterPort(   bootstrap_port,
155			     &masterPort)
156	));
157
158	getClockFrequency();
159
160        return(0);
161}
162