tmp_dir.cpp 3.09 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul Fultz II's avatar
Paul Fultz II committed
24
25
26
#include <migraphx/tmp_dir.hpp>
#include <migraphx/env.hpp>
#include <migraphx/errors.hpp>
27
#include <migraphx/process.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
28
29
30
31
32
33
#include <algorithm>
#include <random>
#include <thread>
#include <sstream>
#include <iostream>
#include <string>
34
35
36
37
38
39
40
41
42

#ifdef _WIN32
// cppcheck-suppress definePrefix
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#undef getpid
// cppcheck-suppress [definePrefix, defineUpperCase]
#define getpid _getpid
#else
Paul Fultz II's avatar
Paul Fultz II committed
43
#include <unistd.h>
44
45
#include <sys/types.h>
#endif
Paul Fultz II's avatar
Paul Fultz II committed
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

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_DEBUG_SAVE_TEMP_DIR)

std::string random_string(std::string::size_type length)
{
    static const std::string& chars = "0123456789"
                                      "abcdefghijklmnopqrstuvwxyz"
                                      "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    std::mt19937 rg{std::random_device{}()};
    std::uniform_int_distribution<std::string::size_type> pick(0, chars.length() - 1);

    std::string str(length, 0);
    std::generate(str.begin(), str.end(), [&] { return chars[pick(rg)]; });

    return str;
}

std::string unique_string(const std::string& prefix)
{
    auto pid = getpid();
    auto tid = std::this_thread::get_id();
71
    auto clk = std::chrono::steady_clock::now().time_since_epoch().count();
Paul Fultz II's avatar
Paul Fultz II committed
72
    std::stringstream ss;
73
    ss << std::hex << prefix << "-" << pid << "-" << tid << "-" << clk << "-" << random_string(16);
Paul Fultz II's avatar
Paul Fultz II committed
74
75
76
    return ss.str();
}

77
78
79
tmp_dir::tmp_dir(const std::string& prefix)
    : path(fs::temp_directory_path() /
           unique_string(prefix.empty() ? "migraphx" : "migraphx-" + prefix))
Paul Fultz II's avatar
Paul Fultz II committed
80
81
82
83
84
85
{
    fs::create_directories(this->path);
}

void tmp_dir::execute(const std::string& exe, const std::string& args) const
{
86
    process{exe + " " + args}.cwd(this->path).exec();
Paul Fultz II's avatar
Paul Fultz II committed
87
88
89
90
}

tmp_dir::~tmp_dir()
{
91
    if(not enabled(MIGRAPHX_DEBUG_SAVE_TEMP_DIR{}))
Paul Fultz II's avatar
Paul Fultz II committed
92
93
94
95
96
97
98
    {
        fs::remove_all(this->path);
    }
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx