summaryrefslogtreecommitdiff
path: root/source/slang-record-replay/record/record-manager.cpp
blob: 3be120c43cf0ed6057e531a1b79f3883caf4feb1 (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
#include "record-manager.h"

#include "../../core/slang-io.h"
#include "../util/record-utility.h"

#include <sstream>
#include <thread>

namespace SlangRecord
{
RecordManager::RecordManager(uint64_t globalSessionHandle)
    : m_recorder(&m_memoryStream)
{
    std::stringstream ss;
    ss << "gs-" << globalSessionHandle << "-t-" << std::this_thread::get_id() << ".cap";

    m_recordFileDirectory = Slang::Path::combine(m_recordFileDirectory, "slang-record");
    if (!Slang::File::exists(m_recordFileDirectory))
    {
        if (!Slang::Path::createDirectoryRecursive(m_recordFileDirectory))
        {
            slangRecordLog(
                LogLevel::Error,
                "Fail to create directory: %s\n",
                m_recordFileDirectory.getBuffer());
        }
    }

    Slang::String recordFilePath =
        Slang::Path::combine(m_recordFileDirectory, Slang::String(ss.str().c_str()));
    m_fileStream = new FileOutputStream(recordFilePath);
}

void RecordManager::clearWithHeader(const ApiCallId& callId, uint64_t handleId)
{
    m_memoryStream.flush();
    FunctionHeader header;
    header.callId = callId;
    header.handleId = handleId;

    // write header to memory stream
    m_memoryStream.write(&header, sizeof(FunctionHeader));
}

void RecordManager::clearWithTailer()
{
    m_memoryStream.flush();
    FunctionTailer tailer;

    // write header to memory stream
    m_memoryStream.write(&tailer, sizeof(FunctionTailer));
}

ParameterRecorder* RecordManager::beginMethodRecord(const ApiCallId& callId, uint64_t handleId)
{
    clearWithHeader(callId, handleId);
    return &m_recorder;
}

ParameterRecorder* RecordManager::endMethodRecord()
{
    FunctionHeader* pHeader = const_cast<FunctionHeader*>(
        reinterpret_cast<const FunctionHeader*>(m_memoryStream.getData()));

    pHeader->dataSizeInBytes = m_memoryStream.getSizeInBytes() - sizeof(FunctionHeader);

    std::hash<std::thread::id> hasher;
    pHeader->threadId = hasher(std::this_thread::get_id());

    // write record data to file
    m_fileStream->write(m_memoryStream.getData(), m_memoryStream.getSizeInBytes());

    // take effect of the write
    m_fileStream->flush();

    // clear the memory stream
    m_memoryStream.flush();

    clearWithTailer();
    return &m_recorder;
}

void RecordManager::apendOutput()
{
    FunctionTailer* pTailer = const_cast<FunctionTailer*>(
        reinterpret_cast<const FunctionTailer*>(m_memoryStream.getData()));

    pTailer->dataSizeInBytes = (uint32_t)(m_memoryStream.getSizeInBytes() - sizeof(FunctionTailer));

    // write record data to file
    m_fileStream->write(m_memoryStream.getData(), m_memoryStream.getSizeInBytes());

    // take effect of the write
    m_fileStream->flush();

    // clear the memory stream
    m_memoryStream.flush();
}
} // namespace SlangRecord