mem_trace.cc revision 11437:210624864179
1360SN/A/*
21458SN/A * Copyright (c) 2015 ARM Limited
3360SN/A * All rights reserved
4360SN/A *
5360SN/A * The license below extends only to copyright in the software and shall
6360SN/A * not be construed as granting a license to any other intellectual
7360SN/A * property including but not limited to intellectual property relating
8360SN/A * to a hardware implementation of the functionality of the software
9360SN/A * licensed hereunder.  You may use the software subject to the license
10360SN/A * terms below provided that you ensure that this notice is replicated
11360SN/A * unmodified and in its entirety in all distributions of the software,
12360SN/A * modified or unmodified, in source code or in binary form.
13360SN/A *
14360SN/A * Redistribution and use in source and binary forms, with or without
15360SN/A * modification, are permitted provided that the following conditions are
16360SN/A * met: redistributions of source code must retain the above copyright
17360SN/A * notice, this list of conditions and the following disclaimer;
18360SN/A * redistributions in binary form must reproduce the above copyright
19360SN/A * notice, this list of conditions and the following disclaimer in the
20360SN/A * documentation and/or other materials provided with the distribution;
21360SN/A * neither the name of the copyright holders nor the names of its
22360SN/A * contributors may be used to endorse or promote products derived from
23360SN/A * this software without specific prior written permission.
24360SN/A *
25360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
272665Ssaidi@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
282665Ssaidi@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
292665Ssaidi@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
322093SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
356712Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
366712Snate@binkert.org *
37360SN/A * Authors: Andreas Hansson
38360SN/A *          Andreas Sandberg
397680Sgblack@eecs.umich.edu */
402474SN/A
41360SN/A#include "mem/probes/mem_trace.hh"
426658Snate@binkert.org
438229Snate@binkert.org#include "base/callback.hh"
442680Sktlim@umich.edu#include "base/output.hh"
458232Snate@binkert.org#include "params/MemTraceProbe.hh"
462474SN/A#include "proto/packet.pb.h"
47360SN/A
488229Snate@binkert.orgMemTraceProbe::MemTraceProbe(MemTraceProbeParams *p)
498229Snate@binkert.org    : BaseMemProbe(p),
506029Ssteve.reinhardt@amd.com      traceStream(nullptr),
51360SN/A      withPC(p->with_pc)
52360SN/A{
532107SN/A    std::string filename;
54360SN/A    if (p->trace_file != "") {
55360SN/A        // If the trace file is not specified as an absolute path,
563114Sgblack@eecs.umich.edu        // append the current simulation output directory
57360SN/A        filename = simout.resolve(p->trace_file);
586702Sgblack@eecs.umich.edu
596701Sgblack@eecs.umich.edu        const std::string suffix = ".gz";
606702Sgblack@eecs.umich.edu        // If trace_compress has been set, check the suffix. Append
616111Ssteve.reinhardt@amd.com        // accordingly.
626111Ssteve.reinhardt@amd.com        if (p->trace_compress &&
637823Ssteve.reinhardt@amd.com            filename.compare(filename.size() - suffix.size(), suffix.size(),
646701Sgblack@eecs.umich.edu                             suffix) != 0)
656701Sgblack@eecs.umich.edu            filename = filename + suffix;
666701Sgblack@eecs.umich.edu    } else {
676701Sgblack@eecs.umich.edu        // Generate a filename from the name of the SimObject. Append .trc
68360SN/A        // and .gz if we want compression enabled.
692680Sktlim@umich.edu        filename = simout.resolve(name() + ".trc" +
70360SN/A                                  (p->trace_compress ? ".gz" : ""));
712495SN/A    }
7210223Ssteve.reinhardt@amd.com
73360SN/A    traceStream = new ProtoOutputStream(filename);
741450SN/A
755958Sgblack@eecs.umich.edu    // Create a protobuf message for the header and write it to
76360SN/A    // the stream
77360SN/A    ProtoMessage::PacketHeader header_msg;
78360SN/A    header_msg.set_obj_id(name());
791450SN/A    header_msg.set_tick_freq(SimClock::Frequency);
803114Sgblack@eecs.umich.edu    traceStream->write(header_msg);
812680Sktlim@umich.edu
82360SN/A    // Register a callback to compensate for the destructor not
831969SN/A    // being called. The callback forces the stream to flush and
842484SN/A    // closes the output file.
852484SN/A    registerExitCallback(
86360SN/A        new MakeCallback<MemTraceProbe, &MemTraceProbe::closeStreams>(this));
87360SN/A}
88360SN/A
891450SN/Avoid
903114Sgblack@eecs.umich.eduMemTraceProbe::closeStreams()
912680Sktlim@umich.edu{
92360SN/A    if (traceStream != NULL)
936701Sgblack@eecs.umich.edu        delete traceStream;
941969SN/A}
956701Sgblack@eecs.umich.edu
96360SN/Avoid
971458SN/AMemTraceProbe::handleRequest(const ProbePoints::PacketInfo &pkt_info)
98360SN/A{
99360SN/A    ProtoMessage::Packet pkt_msg;
100360SN/A
1011450SN/A    pkt_msg.set_tick(curTick());
1028149SChris.Emmons@ARM.com    pkt_msg.set_cmd(pkt_info.cmd.toInt());
1038149SChris.Emmons@ARM.com    pkt_msg.set_flags(pkt_info.flags);
1048149SChris.Emmons@ARM.com    pkt_msg.set_addr(pkt_info.addr);
1058149SChris.Emmons@ARM.com    pkt_msg.set_size(pkt_info.size);
1068149SChris.Emmons@ARM.com    if (withPC && pkt_info.pc != 0)
1078149SChris.Emmons@ARM.com        pkt_msg.set_pc(pkt_info.pc);
1088149SChris.Emmons@ARM.com
1098149SChris.Emmons@ARM.com    traceStream->write(pkt_msg);
1108149SChris.Emmons@ARM.com}
1118149SChris.Emmons@ARM.com
1128149SChris.Emmons@ARM.com
1138149SChris.Emmons@ARM.comMemTraceProbe *
1143114Sgblack@eecs.umich.eduMemTraceProbeParams::create()
1152680Sktlim@umich.edu{
116360SN/A    return new MemTraceProbe(this);
1176029Ssteve.reinhardt@amd.com}
1186029Ssteve.reinhardt@amd.com