CPUtils

This is a small but workable tool that help you manually switch on / off a specific CPU. This tool requires root privilege.

What’s the meaning of this tool? Okay, in fact, I wrote this program just for fun :p
Note: If you switch off number 0 processor (the main processor), you’ll have to do a reboot. Be warned.

Code

//
//  main.c
//  CPUtils
//
//  Created by Ryza 14-9-25.
//  Copyright (c) 2014年 Ryza. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mach/mach.h>

int main(int argc, const char * argv[])
{
        host_t myhost = mach_host_self();
        host_t mypriv;
        kern_return_t kr;
        processor_port_array_t processorPorts;
        mach_msg_type_number_t procCount;
        kr = host_get_host_priv_port(myhost, &mypriv);
        if (kr)
        {
                printf("host_get_host_priv_port: %dn",kr);
                exit(1);
        }
        kr = host_processors(mypriv, &processorPorts, &procCount);
        if (kr)
        {
                printf("host_processors");
                exit(2);
        }
        int * statue = (int *)malloc(sizeof(int) * procCount);
        int mode = 0;
        // -1 -> switch off
        // +1 -> switch on
        for (int i = 1; i < argc; i++)
        {
                if (strcmp(argv[i], "-off") == 0)
                {
                        mode = -1;
                }
                else if (strcmp(argv[i], "-on") == 0)
                {
                        mode = +1;
                }
                else
                {
                        for (int valid = 0; argv[i][valid] != ''; valid++)
                        {
                                if (argv[i][valid] - 48 <0 || argv[i][valid] - 489)
                                {
                                        mode = 0;
                                        break;
                                }
                        }
                        if (mode == 0) continue;
                        int processor = atoi(argv[i]);
                        statue[processor] = mode;
                }
        }
        for (int proc = 0; proc < procCount; proc++)
        {
                switch (statue[proc]) {
                        case -1:
                        {
                                processor_exit(processorPorts[proc]);
                                break;
                        }
                        case 1:
                        {
                                processor_start(processorPorts[proc]);
                                break;
                        }
                        default:
                                break;
                }
                if (kr != KERN_SUCCESS && statue[proc] != 0)
                {
                        printf("Unable to %s %dn",statue[proc] == -1 ? "stop" : "start", proc);
                }
        }
        return 0;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

20 + eleven =