forked from LittleWhite-tb/ftalat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoreRelation.c
159 lines (136 loc) · 4.33 KB
/
CoreRelation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* ftalat - Frequency Transition Latency Estimator
* Copyright (C) 2013 Universite de Versailles
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CoreRelation.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "FreqGetter.h"
#include "FreqSetter.h"
#include "utils.h"
/**
* \struct CoreRelations
* \brief Internal struct to keep the information about cores relations
* The structs contains all the core linked to one.
*/
typedef struct CoreRelations
{
unsigned int nbRelations; /*!< number of elements in coreIDs */
unsigned int* coreIDs; /*!< IDs related to the current core */
}CoreRelations;
CoreRelations* pRelationsTable = NULL;
void initCoreRelations()
{
assert(pRelationsTable == NULL);
unsigned int nbCore = getCoreNumber();
unsigned int i = 0;
// Allocate memory for the table
pRelationsTable = (CoreRelations*) calloc(nbCore, sizeof(CoreRelations));
if ( pRelationsTable == NULL )
{
fprintf(stderr,"Error to allocate memory for relation table\n");
return;
}
for ( i = 0 ; i < nbCore ; i++ )
{
FILE* pRelationFile = openCPUFreqFile(i,"related_cpus","r");
if ( pRelationFile )
{
size_t tabSize = nbCore+1;
pRelationsTable[i].coreIDs = (unsigned int*) malloc(sizeof(unsigned int) * tabSize); // let's say 25 is enough for an init size
if ( pRelationsTable[i].coreIDs != NULL )
{
unsigned int coreID = 0;
size_t counter = 0;
while(fscanf(pRelationFile,"%u",&coreID) == 1 )
{
pRelationsTable[i].coreIDs[counter] = coreID;
counter++;
if ( counter >= tabSize ) // Not enough space remaining
{
// Double size
tabSize *= 2;
unsigned int* newFreqsTab = realloc(pRelationsTable[i].coreIDs,tabSize*sizeof(unsigned int));
if ( newFreqsTab != NULL )
{
pRelationsTable[i].coreIDs = newFreqsTab;
}
else
{
fprintf(stderr,"Fail to allocate more memory for relations table\n");
break;
}
}
}
pRelationsTable[i].nbRelations = counter;
}
else
{
fprintf(stderr,"Fail to allocated memory for line of core relation table\n");
}
fclose(pRelationFile);
}
}
}
void freeCoreRelations()
{
unsigned int nbCore = getCoreNumber();
unsigned int i = 0;
if ( pRelationsTable )
{
for ( i= 0 ; i < nbCore ; i++ )
{
free(pRelationsTable[i].coreIDs);
}
}
free(pRelationsTable);
}
unsigned int getHeadCore(unsigned int coreID)
{
assert(coreID < getCoreNumber());
if ( pRelationsTable[coreID].coreIDs )
{
return pRelationsTable[coreID].coreIDs[0]; // I think the first is always master
}
return 0;
}
void setFreqForAllRelatedCore(unsigned int coreID, unsigned int freq)
{
assert(coreID < getCoreNumber());
if ( pRelationsTable[coreID].coreIDs )
{
unsigned int i = 0;
for ( i = 0 ; i < pRelationsTable[coreID].nbRelations ; i++ )
{
setFreq(pRelationsTable[coreID].coreIDs[i],freq);
}
}
}
void displayCoreRelations(unsigned int coreID)
{
assert(coreID < getCoreNumber());
unsigned int i = 0;
if ( pRelationsTable[coreID].coreIDs )
{
fprintf(stdout,"Relations for core %u : ",coreID);
for ( i = 0 ; i < pRelationsTable[coreID].nbRelations ; i++ )
{
fprintf(stdout,"%u ",pRelationsTable[coreID].coreIDs[i]);
}
fprintf(stdout,"\n");
}
}