3

I'm writing a program in Java

In this program I'm reading and changing an array of data. This is an example of the code:

public double computation() {
    char c = 0;
    char target = 'a';
    int x = 0, y = 1;

    for (int i = 0; i < data.length; i++) {
        // Read Data
        c = data[index[i]];

        if (c == target)
            x++;
        else
            y++;

        //Change Value
        if (Character.isUpperCase(c))
            Character.toLowerCase(c);
        else
            Character.toUpperCase(c);

        //Write Data
        data[index[i]] = c;
    }
    return (double) x / (double) y;
}

BTW, the INDEX array contains DATA array's indexes in random order to prevent prefetching. I'm forcing all of my cache accesses to be missed by using random indexes in INDEX array.

Now I want to check what is the behavior of the CPU cache by collecting information about its hit ratio.

Is there any developed tool for this purpose? If not is there any technique?

aaaidan
  • 7,093
  • 8
  • 66
  • 102
Reza
  • 2,058
  • 3
  • 20
  • 33

2 Answers2

1

I don't think you can reach such low level information from Java but someone might know better. You could write the same program with no cache misses and check the difference. This is what I suggested in this other post for example.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
  • How can I be sure that all of my data has been cached? I don't know if you get the purpose of this code or not, but I'm forcing all of my cache accesses to be missed by using random indexes in INDEX array. I mean your suggestion seems good, but shouldn't there be a tool for such purposes? – Reza Oct 08 '12 at 20:23
  • @Hesam Can you give an example of where a real world program would need to know this? – Peter Lawrey Oct 08 '12 at 20:25
  • @PeterLawrey Anywhere, where you need to have a high speed computation! In my case, thread migration will make a lot of overhead, depending on cache behavior. I think I should check the hit ratio of my cache accesses in order to have an the best performance. – Reza Oct 08 '12 at 20:28
  • @PeterLawrey, Just to make myself clear, I somehow found a tool but there was no link for downloading the tool. [THOR](http://dl.acm.org/citation.cfm?id=1940704) – Reza Oct 08 '12 at 20:38
  • @Hesam To address this problem, I have used thread affinity to assign critical threads to cpus to prevent migration. I don't see how know how badly your system is performing without any means of controlling it. – Peter Lawrey Oct 08 '12 at 20:43
  • @PeterLawrey, In fact I've seen and used your library recently. [ThreadAffinity](https://github.com/peter-lawrey/Java-Thread-Affinity) :D. I'm working on an experimental piece of code! – Reza Oct 08 '12 at 20:47
1

On Linux it is possible to collect such information via OProfile. Each CPU has performance event counters. See here for the list of the AMD K15 family events: http://oprofile.sourceforge.net/docs/amd-family15h-events.php

OProfile regularly samples the event counter(s) and together with the program counter. After a program run you can analyze how many events happen and at (statistically) what program position.

OProfile has build in Java support. It interacts with the Java JIT and creates a synthetic symbol table to look up the Java method name for a peace of generated JIT code.

The initial setup is not quite easy. If interested, I can guide you through or write a little more about it.

cruftex
  • 5,545
  • 2
  • 20
  • 36
  • This was an old project of mine 2 years ago. I used [PAPI](http://icl.cs.utk.edu/papi/) at the time which did not provide the most accurate results but it was good for my purpose. I hope your answer will help another fellow. – Reza Nov 18 '14 at 18:22