`
alexela
  • 浏览: 4684 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Compute the CPU frequency by Java

阅读更多

By my previous article of "How to control the CPU occupation rate", someone came up with another idea - "How to get or compute the CPU frequency when I run the program in different PCs?"

That's really a challenge! But it's not a big problem by the same theory of CPU occupation control. We know now that CPU can run about 2 directives per tick, and the frequency is the count of ticks per second, and each for(i=0;i<n;i++) cycle contains about more than 5 ASM directives. By these estimated parameters, we will be able to compute the CPU frequency via a reverse computation engineering.

The source code is showed as below:

/* GetCPUFrequency.java
 *
 * TSUSOFT.NET, ALEXELA.CN
 * Copyright (C) 2007 by Alexandre Tsu
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the License.
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
 * provisions of LGPL are applicable instead of those above. If you wish to
 * allow use of your version of this file only under the terms of the LGPL
 * License and not to allow others to use your version of this file under
 * the MPL, indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by the LGPL.
 * If you do not delete the provisions above, a recipient may use your version
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
 *
 * Created on Aug 21, 2007, by Alexandre Tsu.
 *
 */


public class GetCPUFrequency
{
    public static void main(String[] args)
    {
        try
        {
            if(args.length != 2)
                printUsage();

            /* parameters estimation from input */
            float dpt = Float.parseFloat(args[0]);
            float dpc = Float.parseFloat(args[1]);

            System.out.print("Hit <Enter> to go on!");
            System.in.read();

            /* run cycles for Integer.MAX_VALUE times */
            long l1 = System.currentTimeMillis();
            for(int i=0; i<Integer.MAX_VALUE; i++);
            long l2 = System.currentTimeMillis();

            /* compute the cycles per second */
            long cps = Integer.MAX_VALUE / ( (l2 - l1) / 1000 );

            /* output the computation result */
            System.out.println("Directives per tick: " + dpt);
            System.out.println("Directives per cycle: " + dpc);
            System.out.println("Cycles per second: " + cps);
            System.out.println("CPU Frequency at: " + (cps * dpc / dpt));
        }
        catch(Throwable e)
        {
            printUsage();
        }
    }

    private static void printUsage()
    {
        System.out.println("Usage: java GetCPUFrequency dpt dpc");
        System.out.println("dpt: directives per tick");
        System.out.println("dpc: directives per cycle");
        System.exit(1);
    }
}

I tried running this program in different PCs (one with Windows and anther with Linux). I got a close result to my real CPU frequency. For my pratice, I estimated the two input parameters as 2 and 15. My actual CPU frequency is 1.40Ghz. One of run situation is as below:

D:\workspace\Test>java GetCPUFrequency 2 15
Hit <Enter> to go on!
Directives per tick: 2.0
Directives per cycle: 15.0
Cycles per second: 195225786
CPU Frequency at: 1.46419341E9

分享到:
评论

相关推荐

    云计算 02.Java SDK for FusionCompute

    描述:R5版本FusionCompute JAVA API接口,提供随需使用的虚拟化计算能力,以及为支撑计算能力而必须配套的存储及网络能力。 1.文档放在doc中,包含开发指南和接口参考。 2.程序放在software中,提供形态为Java ...

    compute by与compute

    compute by与compute.doc

    云计算 01.Java SDK for FusionCompute

    描述:iOS版本eSpace控件接口,提供IM、呼叫、会议、联系人及群组等相关能力。 1.文档放在doc中,包含控件使用指南。 2.程序放在software中,当前提供iPhone版本控件程序包。 3.编程代码样例放在demo中,包含demo...

    C++ AMP for the DirectCompute Programmer

    DirectCompute draws a sharp distinction between the code that runs on the host (typical C or C++) and the code that runs on the device (C-like HLSL kernel code). The host code and the device code ...

    eSDK Cloud V100R005C10 代码样例 01(FusionCompute, R3, REST, Java)

    介绍eSDK Cloud 基于FusionCompute R3接口通过Java语言编程代码示例。

    eSDK Cloud V100R005C10 代码样例 01(FusionCompute, R5, REST, Java)

    介绍eSDK Cloud 基于FusionCompute R5接口通过Java语言编程代码示例。

    java实验4.3(SalesArray.java )

    Design and write a Java class named SalesArray.java that will: 1. Declare a two-dimensional integer array named sales. Populate the first four columns using the following data. Quarter 1 Quarter 2 ...

    Chapter 13 The Compute Shader

    Introduction to 3D Game Programming with Directx12 Chapter13 习题解答源码

    esb java compute节点连接oracle做CURD

    esb java compute节点连接oracle做CURD;esb java compute节点连接oracle做CURD

    java实验7.1(CollegeCourse.java)

    Run the example by going to c:\java\lab7 and typing: java ynu/edu/lab7/UseCourse For example, the output could be: The cost of your course is $180.0 there is a $25.00 lab fee. The cost of your ...

    探讨SQL compute by的使用分析

    如果想在SQL SERVER中完成这项工作,可以使用COMPUTE BY子句。COMPTE生成合计作为附加的汇总列出现在结果集的最后。当与BY一起使用时,COMPUTE 子句在结果集内生成控制中断和分类汇总。 下列 SELECT 语句使用简单 ...

    Development of an Optimal Vehicle-to-Grid Aggregator for Frequency Regulation

    For vehicle-to-grid (V2G) frequency regulation services, we propose an aggregator that makes efficient use of the distributed power of electric vehicles to produce the desired grid-scale power....

    FusionCompute安装流程

    华为FusionCompute 8.0.0版本的安装流程,其中有安装CNA,VRM 操作步骤,服务器配置要求,本地PC安装FusionCompute安装包过程等

    华为云计算FusionCompute8 8.0.1 8.0.1.SPH2 FusionSphere8 全套资源

    FusionCompute8.0.0目录 FusionCompute_Installer-8.0.0.zip ---------- Windows安装工具 FusionCompute_CNA-8.0.0-X86_64.iso ------------ CNA计算节点安装镜像(操作系统) FusionCompute_CNA-8.0.0-ARM_64.iso -...

    Sparsity_SDOCT_Software_2012.rar_MSBTD_OCT眼底稀疏性去噪分割_The First_sd

    Main.m: the file to run the software Instructions how to use the GUI_interface Step 1: click the 'open test' button to ...Step 6: compute the CNR by first select a background region from the test image

    FusionCompute 6.5.1虚拟化套件(包含CNA、VRM)

    FusionCompute 6.5.1虚拟化套件(包含CNA、VRM等) 1. FusionCompute 6.5.1_CNA.iso 2. FusionCompute 6.5.1_GpuCompiler.zip 3. FusionCompute 6.5.1_Installer.zip 4. FusionCompute 6.5.1_VncViewer.zip 5. ...

    JavaChineseCalendar

    Because **農曆** object has no hour information, if you want to compute the weight of Ba Zi, you have to provide the hour information in the form of the twelve Earthly Branches. float BaZiWeight = ...

    使用DirectCompute 实现数字滤波

    使用CPU + GPU的异构模式,实现单通道数字滤波(FIR).其实现采用CPU实现和GPU实现,可以对比两种方法的结果。主要采用了DirectCompute框架,在VS2008 + DX11 + Win7下编译通过,显卡GForce 9600. VC完整工程,独立...

    Java代码.zip

    (3)编写一个Java Application程序Compute.java,用来显示某一个圆柱体中心坐标位置(如图1中的k的位置),高度h,以及它的表面积(不包括两个底的面积)、体积和底面积。要求圆半径r,圆心坐标x和y,以及高度h的值均从...

    FusionCompute8.0

    完整的 FusionCompute,OpenStack,VRM,CNA、installer等整套FusionSphere配套软件,通过百度网盘下载连接密码永久生效持续免费更新。测试使用,非商用

Global site tag (gtag.js) - Google Analytics