~ubuntu-branches/ubuntu/precise/libpgm/precise

« back to all changes in this revision

Viewing changes to openpgm/pgm/examples/heatmap.c

  • Committer: Bazaar Package Importer
  • Author(s): Gabriel de Perthuis
  • Date: 2011-04-07 16:48:52 UTC
  • Revision ID: james.westby@ubuntu.com-20110407164852-8uamem42ojeptj6l
Tags: upstream-5.1.116~dfsg
ImportĀ upstreamĀ versionĀ 5.1.116~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdint.h>
 
2
#include <stdio.h>
 
3
#include <stdlib.h>
 
4
#include <arpa/inet.h>
 
5
 
 
6
const char header[] =
 
7
        "set title \"PGM Latency Heat Map\"\n"
 
8
        "unset key\n"
 
9
        "set tic scale 0\n"
 
10
        "\n"
 
11
        "# Colour runs from white to green\n"
 
12
        "set palette rgbformula -6,2,-3\n"
 
13
        "set cbrange [0:25]\n"
 
14
        "set cblabel \"Density\"\n"
 
15
        "unset cbtics\n"
 
16
        "\n"
 
17
        "set xrange [-0.5:%u.5]\n"
 
18
        "set yrange [-0.5:%u.5]\n"
 
19
        "\n"
 
20
        "set view map\n"
 
21
        "plot '-' using 1:2:3 with image\n";
 
22
const char footer[] =
 
23
        "e\n";
 
24
 
 
25
const unsigned yrange_max = 2*1000;
 
26
const unsigned yrange_ivl = 10;
 
27
 
 
28
static
 
29
int
 
30
key_compare (
 
31
        const void*     p1,
 
32
        const void*     p2
 
33
        )
 
34
{
 
35
        const uint32_t i = *((uint32_t*)p1);
 
36
        const uint32_t j = *((uint32_t*)p2);
 
37
 
 
38
        if (i > j)
 
39
                return 1;
 
40
        if (i < j)
 
41
                return -1;
 
42
        return 0;
 
43
}
 
44
 
 
45
int main (void) {
 
46
        const char src[] = "/tmp/heat.dmp";
 
47
        FILE* fh;
 
48
        uint32_t slice_count, alloc_count = 0;
 
49
        uint32_t* words;
 
50
        unsigned slice = 0, xrange_max = 0, density_max = 0;
 
51
 
 
52
        if (NULL == (fh = fopen (src, "r"))) {
 
53
                perror ("fopen");
 
54
                return EXIT_FAILURE;
 
55
        }
 
56
 
 
57
/* find time period of dump */
 
58
        while (1) {
 
59
                if (1 != fread (&slice_count, sizeof (slice_count), 1, fh)) {
 
60
                        break;
 
61
                }
 
62
                slice_count = ntohl (slice_count);
 
63
                if (0 == alloc_count) {
 
64
                        alloc_count = slice_count;
 
65
                        words = malloc (alloc_count * sizeof (uint32_t) * 2);
 
66
                } else if (slice_count > alloc_count) {
 
67
                        alloc_count = slice_count;
 
68
                        words = realloc (words, alloc_count * sizeof (uint32_t) * 2);
 
69
                }
 
70
                if (slice_count != fread (words, sizeof (uint32_t) * 2, slice_count, fh)) {
 
71
                        perror ("words");
 
72
                        goto abort;
 
73
                }
 
74
                for (int_fast32_t i = slice_count - 1; i >= 0; i--) {
 
75
                        const unsigned density = ntohl (words[(2*i)+1]);
 
76
                        if (density > density_max)
 
77
                                density_max = density;
 
78
                }
 
79
                xrange_max++;
 
80
        }
 
81
        rewind (fh);
 
82
 
 
83
        printf (header, xrange_max - 1, yrange_max - yrange_ivl);
 
84
 
 
85
        while (1) {
 
86
                if (1 != fread (&slice_count, sizeof (slice_count), 1, fh)) {
 
87
                        break;
 
88
                }
 
89
                slice_count = ntohl (slice_count);
 
90
                if (slice_count != fread (words, sizeof (uint32_t) * 2, slice_count, fh)) {
 
91
                        perror ("words");
 
92
                        goto abort;
 
93
                }
 
94
                for (int_fast32_t i = slice_count - 1; i >= 0; i--) {
 
95
                        words[(2*i)+0] = ntohl (words[(2*i)+0]);
 
96
                        words[(2*i)+1] = ((ntohl (words[(2*i)+1]) * 25) + (density_max - 1)) / density_max;
 
97
                }
 
98
                qsort ((void*)words, slice_count, sizeof (uint32_t) * 2, key_compare);
 
99
                for (int_fast32_t i = 0, j = 0; i < slice_count; i++) {
 
100
                        while ((j * yrange_ivl) < words[(2*i)+0]) {
 
101
                                if ((j * yrange_ivl) >= yrange_max)
 
102
                                        goto end_slice;
 
103
                                printf ("%u %u 0\n",
 
104
                                        (unsigned)slice,
 
105
                                        (unsigned)(j * yrange_ivl));
 
106
                                j++;
 
107
                        }
 
108
                        if ((j * yrange_ivl) >= yrange_max)
 
109
                                goto end_slice;
 
110
                        printf ("%u %u %u\n",
 
111
                                (unsigned)slice,
 
112
                                (unsigned)(j * yrange_ivl),
 
113
                                (unsigned)words[(2*i)+1]);
 
114
                        j++;
 
115
                }
 
116
                for (int_fast32_t j = words[(2*(slice_count-1))+0] + yrange_ivl; j < yrange_max; j += yrange_ivl) {
 
117
                        printf ("%u %u 0\n",
 
118
                                (unsigned)slice,
 
119
                                (unsigned)(j));
 
120
                }
 
121
end_slice:
 
122
                putchar ('\n');
 
123
                slice++;
 
124
        }
 
125
 
 
126
        puts (footer);
 
127
 
 
128
abort:
 
129
        fclose (fh);
 
130
        return EXIT_SUCCESS;
 
131
}