Skip to content

Commit a21df71

Browse files
committed
[service] Add a CompilerGymServiceContext class.
Issue #591.
1 parent d5d60b6 commit a21df71

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

compiler_gym/service/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ cc_library(
3737
],
3838
)
3939

40+
cc_library(
41+
name = "CompilerGymServiceContext",
42+
srcs = ["CompilerGymServiceContext.cc"],
43+
hdrs = ["CompilerGymServiceContext.h"],
44+
visibility = ["//visibility:public"],
45+
deps = [
46+
"//compiler_gym/service/proto:compiler_gym_service_cc",
47+
"@boost//:filesystem",
48+
"@com_github_grpc_grpc//:grpc++",
49+
],
50+
)
51+
4052
py_library(
4153
name = "connection",
4254
srcs = ["connection.py"],
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Facebook, Inc. and its affiliates.
2+
//
3+
// This source code is licensed under the MIT license found in the
4+
// LICENSE file in the root directory of this source tree.
5+
#pragma once
6+
7+
#include "compiler_gym/service/CompilerGymServiceContext.h"
8+
9+
using grpc::Status;
10+
11+
namespace compiler_gym {
12+
13+
CompilerGymServiceContext::CompilerGymServiceContext(
14+
const boost::filesystem::path& workingDirectory)
15+
: workingDirectory_(workingDirectory) {}
16+
17+
virtual Status CompilerGymServiceContext::init() { return Status::OK; }
18+
19+
virtual Status CompilerGymServiceContext::shutdown() { return Status::OK; }
20+
21+
} // namespace compiler_gym
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright (c) Facebook, Inc. and its affiliates.
2+
//
3+
// This source code is licensed under the MIT license found in the
4+
// LICENSE file in the root directory of this source tree.
5+
#pragma once
6+
7+
#include <grpcpp/grpcpp.h>
8+
9+
#include "boost/filesystem.hpp"
10+
11+
namespace compiler_gym {
12+
13+
/**
14+
* Execution context of a compiler gym service.
15+
*
16+
* This class encapsulates mutable state that is shared between all compilation
17+
* sessions. An instance of this class is passed to every new
18+
* CompilationSession.
19+
*
20+
* You may subclass CompilerGymServiceContext to add additional mutable state.
21+
* The subclass .
22+
*
23+
* \code{.cpp}
24+
*
25+
* #include "compiler_gym/service/CompilationSession.h"
26+
* #include "compiler_gym/service/CompilerGymServiceContext.h"
27+
* #include "compiler_gym/service/runtime/Runtime.h"
28+
*
29+
* using namespace compiler_gym;
30+
*
31+
* class MyServiceContext final : public ServiceContext { ... }
32+
*
33+
* class MyCompilationSession final : public CompilationSession { ... }
34+
*
35+
* int main(int argc, char** argv) {
36+
* return runtime::createAndRunCompilerGymService<MyCompilationSession, MyServiceContext>();
37+
* }
38+
* \endcode
39+
*/
40+
class CompilerGymServiceContext {
41+
public:
42+
CompilerGymServiceContext(const boost::filesystem::path& workingDirectory);
43+
44+
/**
45+
* Initialize context.
46+
*
47+
* Called before any compilation sessions are created. Use this method to
48+
* initialize any mutable state. If this routine returns an error, the service
49+
* will terminate.
50+
*
51+
* @return A status.
52+
*/
53+
[[nodiscard]] virtual grpc::Status init();
54+
55+
/**
56+
* Uninitialize context.
57+
*
58+
* Called after all compilation sessions have ended, before a service
59+
* terminates. Use this method to perform tidying up. This method is always
60+
* called, even if init() fails. If this routine returns an error, the service
61+
* will terminate with a nonzero error code.
62+
*
63+
* @return A status.
64+
*/
65+
[[nodiscard]] virtual grpc::Status shutdown();
66+
67+
/**
68+
* Get the working directory.
69+
*
70+
* The working directory is a local filesystem directory that compilation
71+
* sessions can use to store temporary files such as build artifacts. The
72+
* directory is guaranteed to exist.
73+
*
74+
* \note When possible, an in-memory filesystem will be used for the working
75+
* directory. This means that the working directory is not suitable for
76+
* very large files or executables, as some systems prevent execution of
77+
* in-memory files.
78+
*
79+
* \note A single working directory is shared by all of the compilation
80+
* sessions of a service. Do not assume that you have exclusive access.
81+
*
82+
* @return A path.
83+
*/
84+
inline const boost::filesystem::path& workingDirectory() const { return workingDirectory_; };
85+
86+
private:
87+
const boost::filesystem::path workingDirectory_;
88+
};
89+
90+
} // namespace compiler_gym

0 commit comments

Comments
 (0)