summaryrefslogtreecommitdiff
path: root/src/mem/cache/tags/split.cc
blob: bc74f0e0f6d7776a1f58595256d45ef1c5c393e9 (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
 * Copyright (c) 2004-2005 The Regents of The University of Michigan
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Lisa Hsu
 */

/**
 * @file
 * Definitions of split cache tag store.
 */

#include <string>
#include <iostream>
#include <fstream>

#include "base/cprintf.hh"
#include "base/intmath.hh"
#include "base/output.hh"
#include "base/trace.hh"
#include "mem/cache/base_cache.hh"
#include "mem/cache/tags/split.hh"
#include "mem/cache/tags/split_lifo.hh"
#include "mem/cache/tags/split_lru.hh"


using namespace std;
using namespace TheISA;

// create and initialize a partitioned cache structure
Split::Split(int _numSets, int _blkSize, int total_ways, int LRU1_assoc,
             bool _lifo, bool _two_queue, int _hit_latency) :
    numSets(_numSets), blkSize(_blkSize), lifo(_lifo), hitLatency(_hit_latency)
{
    DPRINTF(Split, "new split cache!!\n");

    DPRINTF(Split, "lru has %d numSets, %d blkSize, %d assoc, and %d hit_latency\n",
            numSets, blkSize, LRU1_assoc, hitLatency);

    lru = new SplitLRU(_numSets, _blkSize, LRU1_assoc, _hit_latency, 1);

    if (total_ways - LRU1_assoc == 0) {
        lifo_net = NULL;
        lru_net = NULL;
    } else {
        if (lifo) {
            DPRINTF(Split, "Other partition is a LIFO with size %d in bytes. it gets %d ways\n",
                    (total_ways - LRU1_assoc)*_numSets*_blkSize, (total_ways - LRU1_assoc));
            lifo_net = new SplitLIFO(_blkSize, (total_ways - LRU1_assoc)*_numSets*_blkSize,
                                     (total_ways - LRU1_assoc), _hit_latency, _two_queue, 2);
            lru_net = NULL;
        }
        else {
            DPRINTF(Split, "other LRU gets %d ways\n", total_ways - LRU1_assoc);
            lru_net = new SplitLRU(_numSets, _blkSize, total_ways - LRU1_assoc, _hit_latency, 2);
            lifo_net = NULL;
        }
    }

    blkMask = blkSize - 1;

    if (!isPowerOf2(total_ways))
        warn("total cache ways/columns %d should be power of 2",
             total_ways);

    warmedUp = false;
    /** @todo Make warmup percentage a parameter. */
    warmupBound = numSets * total_ways;

}

Split::~Split()
{
    delete lru;
    if (lifo)
        delete lifo_net;
    else
        delete lru_net;
}

void
Split::regStats(const string &name)
{
    using namespace Stats;

    BaseTags::regStats(name);

    usedEvictDist.init(0,3000,40);
    unusedEvictDist.init(0,3000,40);
    useByCPUCycleDist.init(0,35,1);

    nic_repl
        .name(name + ".nic_repl")
        .desc("number of replacements in the nic partition")
        .precision(0)
        ;

    cpu_repl
        .name(name + ".cpu_repl")
        .desc("number of replacements in the cpu partition")
        .precision(0)
        ;

    lru->regStats(name + ".lru");

    if (lifo && lifo_net) {
        lifo_net->regStats(name + ".lifo_net");
    } else if (lru_net) {
        lru_net->regStats(name + ".lru_net");
    }

    nicUsedWhenEvicted
        .name(name + ".nicUsedWhenEvicted")
        .desc("number of NIC blks that were used before evicted")
        ;

    nicUsedTotLatency
        .name(name + ".nicUsedTotLatency")
        .desc("total cycles before eviction of used NIC blks")
        ;

    nicUsedTotEvicted
        .name(name + ".nicUsedTotEvicted")
        .desc("total number of used NIC blks evicted")
        ;

    nicUsedAvgLatency
        .name(name + ".nicUsedAvgLatency")
        .desc("avg number of cycles a used NIC blk is in cache")
        .precision(0)
        ;
    nicUsedAvgLatency = nicUsedTotLatency / nicUsedTotEvicted;

    usedEvictDist
        .name(name + ".usedEvictDist")
        .desc("distribution of used NIC blk eviction times")
        .flags(pdf | cdf)
        ;

    nicUnusedWhenEvicted
        .name(name + ".nicUnusedWhenEvicted")
        .desc("number of NIC blks that were unused when evicted")
        ;

    nicUnusedTotLatency
        .name(name + ".nicUnusedTotLatency")
        .desc("total cycles before eviction of unused NIC blks")
        ;

    nicUnusedTotEvicted
        .name(name + ".nicUnusedTotEvicted")
        .desc("total number of unused NIC blks evicted")
        ;

    nicUnusedAvgLatency
        .name(name + ".nicUnusedAvgLatency")
        .desc("avg number of cycles an unused NIC blk is in cache")
        .precision(0)
        ;
    nicUnusedAvgLatency = nicUnusedTotLatency / nicUnusedTotEvicted;

    unusedEvictDist
        .name(name + ".unusedEvictDist")
        .desc("distribution of unused NIC blk eviction times")
        .flags(pdf | cdf)
        ;

    nicUseByCPUCycleTotal
        .name(name + ".nicUseByCPUCycleTotal")
        .desc("total latency of NIC blks til usage time")
        ;

    nicBlksUsedByCPU
        .name(name + ".nicBlksUsedByCPU")
        .desc("total number of NIC blks used")
        ;

    nicAvgUsageByCPULatency
        .name(name + ".nicAvgUsageByCPULatency")
        .desc("average number of cycles before a NIC blk that is used gets used")
        .precision(0)
        ;
    nicAvgUsageByCPULatency = nicUseByCPUCycleTotal / nicBlksUsedByCPU;

    useByCPUCycleDist
        .name(name + ".useByCPUCycleDist")
        .desc("the distribution of cycle time in cache before NIC blk is used")
        .flags(pdf | cdf)
        ;

    cpuUsedBlks
        .name(name + ".cpuUsedBlks")
        .desc("number of cpu blks that were used before evicted")
        ;

    cpuUnusedBlks
        .name(name + ".cpuUnusedBlks")
        .desc("number of cpu blks that were unused before evicted")
        ;

    nicAvgLatency
        .name(name + ".nicAvgLatency")
        .desc("avg number of cycles a NIC blk is in cache before evicted")
        .precision(0)
        ;
    nicAvgLatency = (nicUnusedTotLatency + nicUsedTotLatency) /
        (nicUnusedTotEvicted + nicUsedTotEvicted);

    NR_CP_hits
        .name(name + ".NR_CP_hits")
        .desc("NIC requests hitting in CPU Partition")
        ;

    NR_NP_hits
        .name(name + ".NR_NP_hits")
        .desc("NIC requests hitting in NIC Partition")
        ;

    CR_CP_hits
        .name(name + ".CR_CP_hits")
        .desc("CPU requests hitting in CPU partition")
        ;

    CR_NP_hits
        .name(name + ".CR_NP_hits")
        .desc("CPU requests hitting in NIC partition")
        ;

}

// probe cache for presence of given block.
bool
Split::probe(Addr addr) const
{
    bool success = lru->probe(addr);
    if (!success) {
        if (lifo && lifo_net)
            success = lifo_net->probe(addr);
        else if (lru_net)
            success = lru_net->probe(addr);
    }

    return success;
}

SplitBlk*
Split::findBlock(PacketPtr &pkt, int &lat)
{

    Addr aligned = blkAlign(pkt->getAddr());

    if (memHash.count(aligned)) {
        memHash[aligned]++;
    } else if (pkt->nic_pkt()) {
        memHash[aligned] = 1;
    }

    SplitBlk *blk = lru->findBlock(pkt->getAddr(), lat);
    if (blk) {
        if (pkt->nic_pkt()) {
            NR_CP_hits++;
        } else {
            CR_CP_hits++;
        }
    } else {
        if (lifo && lifo_net) {
            blk = lifo_net->findBlock(pkt->getAddr(), lat);

        } else if (lru_net) {
            blk = lru_net->findBlock(pkt->getAddr(), lat);
        }
        if (blk) {
            if (pkt->nic_pkt()) {
                NR_NP_hits++;
            } else {
                CR_NP_hits++;
            }
        }
    }

    if (blk) {
        Tick latency = curTick - blk->ts;
        if (blk->isNIC) {
            if (!blk->isUsed && !pkt->nic_pkt()) {
                    useByCPUCycleDist.sample(latency);
                    nicUseByCPUCycleTotal += latency;
                    nicBlksUsedByCPU++;
            }
        }
        blk->isUsed = true;

        if (pkt->nic_pkt()) {
            DPRINTF(Split, "found block in partition %d\n", blk->part);
        }
    }
    return blk;
}

SplitBlk*
Split::findBlock(Addr addr, int &lat)
{
    SplitBlk *blk = lru->findBlock(addr, lat);
    if (!blk) {
        if (lifo && lifo_net) {
            blk = lifo_net->findBlock(addr, lat);
        } else if (lru_net) {
            blk = lru_net->findBlock(addr, lat);
        }
    }

    return blk;
}

SplitBlk*
Split::findBlock(Addr addr) const
{
    SplitBlk *blk = lru->findBlock(addr);
    if (!blk) {
        if (lifo && lifo_net) {
            blk = lifo_net->findBlock(addr);
        } else if (lru_net) {
            blk = lru_net->findBlock(addr);
        }
    }

    return blk;
}

SplitBlk*
Split::findReplacement(PacketPtr &pkt, PacketList &writebacks,
                     BlkList &compress_blocks)
{
    SplitBlk *blk;

    if (pkt->nic_pkt()) {
        DPRINTF(Split, "finding a replacement for nic_req\n");
        nic_repl++;
        if (lifo && lifo_net)
            blk = lifo_net->findReplacement(pkt, writebacks,
                                             compress_blocks);
        else if (lru_net)
            blk = lru_net->findReplacement(pkt, writebacks,
                                            compress_blocks);
        // in this case, this is an LRU only cache, it's non partitioned
        else
            blk = lru->findReplacement(pkt, writebacks, compress_blocks);
    } else {
        DPRINTF(Split, "finding replacement for cpu_req\n");
        blk = lru->findReplacement(pkt, writebacks,
                                    compress_blocks);
        cpu_repl++;
    }

    Tick latency = curTick - blk->ts;
    if (blk->isNIC) {
        if (blk->isUsed) {
            nicUsedWhenEvicted++;
            usedEvictDist.sample(latency);
            nicUsedTotLatency += latency;
            nicUsedTotEvicted++;
        } else {
            nicUnusedWhenEvicted++;
            unusedEvictDist.sample(latency);
            nicUnusedTotLatency += latency;
            nicUnusedTotEvicted++;
        }
    } else {
        if (blk->isUsed) {
            cpuUsedBlks++;
        } else {
            cpuUnusedBlks++;
        }
    }

    // blk attributes for the new blk coming IN
    blk->ts = curTick;
    blk->isNIC = (pkt->nic_pkt()) ? true : false;

    return blk;
}

void
Split::invalidateBlk(Addr addr)
{
    SplitBlk *blk = lru->findBlock(addr);
    if (!blk) {
        if (lifo && lifo_net)
            blk = lifo_net->findBlock(addr);
        else if (lru_net)
            blk = lru_net->findBlock(addr);

        if (!blk)
            return;
    }

    blk->status = 0;
    blk->isTouched = false;
    tagsInUse--;
}

void
Split::cleanupRefs()
{
    lru->cleanupRefs();
    if (lifo && lifo_net)
        lifo_net->cleanupRefs();
    else if (lru_net)
        lru_net->cleanupRefs();

    ofstream memPrint(simout.resolve("memory_footprint.txt").c_str(),
                      ios::trunc);

    // this shouldn't be here but it happens at the end, which is what i want
    memIter end = memHash.end();
    for (memIter iter = memHash.begin(); iter != end; ++iter) {
        ccprintf(memPrint, "%8x\t%d\n", (*iter).first, (*iter).second);
    }
}

Addr
Split::regenerateBlkAddr(Addr tag, int set) const
{
    if (lifo_net)
        return lifo_net->regenerateBlkAddr(tag, set);
    else
        return lru->regenerateBlkAddr(tag, set);
}

Addr
Split::extractTag(Addr addr, SplitBlk *blk) const
{
    if (blk->part == 2) {
        if (lifo_net)
            return lifo_net->extractTag(addr);
        else if (lru_net)
            return lru_net->extractTag(addr);
        else
            panic("this shouldn't happen");
    } else
        return lru->extractTag(addr);
}