Skip to content

Commit

Permalink
[service] Add a CompilerGymServiceContext class.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisCummins committed Mar 17, 2022
1 parent c5b9289 commit 3363c2d
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
12 changes: 12 additions & 0 deletions compiler_gym/service/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ cc_library(
],
)

cc_library(
name = "CompilerGymServiceContext",
srcs = ["CompilerGymServiceContext.cc"],
hdrs = ["CompilerGymServiceContext.h"],
visibility = ["//visibility:public"],
deps = [
"//compiler_gym/service/proto:compiler_gym_service_cc",
"@boost//:filesystem",
"@com_github_grpc_grpc//:grpc++",
],
)

py_library(
name = "connection",
srcs = ["connection.py"],
Expand Down
21 changes: 21 additions & 0 deletions compiler_gym/service/CompilerGymServiceContext.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#pragma once

#include "compiler_gym/service/CompilerGymServiceContext.h"

using grpc::Status;

namespace compiler_gym {

CompilerGymServiceContext::CompilerGymServiceContext(
const boost::filesystem::path& workingDirectory)
: workingDirectory_(workingDirectory) {}

virtual Status CompilerGymServiceContext::init() { return Status::OK; }

virtual Status CompilerGymServiceContext::shutdown() { return Status::OK; }

} // namespace compiler_gym
90 changes: 90 additions & 0 deletions compiler_gym/service/CompilerGymServiceContext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#pragma once

#include <grpcpp/grpcpp.h>

#include "boost/filesystem.hpp"

namespace compiler_gym {

/**
* Execution context of a compiler gym service.
*
* This class encapsulates mutable state that is shared between all compilation
* sessions. An instance of this class is passed to every new
* CompilationSession.
*
* You may subclass CompilerGymServiceContext to add additional mutable state.
* The subclass .
*
* \code{.cpp}
*
* #include "compiler_gym/service/CompilationSession.h"
* #include "compiler_gym/service/CompilerGymServiceContext.h"
* #include "compiler_gym/service/runtime/Runtime.h"
*
* using namespace compiler_gym;
*
* class MyServiceContext final : public ServiceContext { ... }
*
* class MyCompilationSession final : public CompilationSession { ... }
*
* int main(int argc, char** argv) {
* return runtime::createAndRunCompilerGymService<MyCompilationSession, MyServiceContext>();
* }
* \endcode
*/
class CompilerGymServiceContext {
public:
CompilerGymServiceContext(const boost::filesystem::path& workingDirectory);

/**
* Initialize context.
*
* Called before any compilation sessions are created. Use this method to
* initialize any mutable state. If this routine returns an error, the service
* will terminate.
*
* @return A status.
*/
[[nodiscard]] virtual grpc::Status init();

/**
* Uninitialize context.
*
* Called after all compilation sessions have ended, before a service
* terminates. Use this method to perform tidying up. This method is always
* called, even if init() fails. If this routine returns an error, the service
* will terminate with a nonzero error code.
*
* @return A status.
*/
[[nodiscard]] virtual grpc::Status shutdown();

/**
* Get the working directory.
*
* The working directory is a local filesystem directory that compilation
* sessions can use to store temporary files such as build artifacts. The
* directory is guaranteed to exist.
*
* \note When possible, an in-memory filesystem will be used for the working
* directory. This means that the working directory is not suitable for
* very large files or executables, as some systems prevent execution of
* in-memory files.
*
* \note A single working directory is shared by all of the compilation
* sessions of a service. Do not assume that you have exclusive access.
*
* @return A path.
*/
inline const boost::filesystem::path& workingDirectory() const { return workingDirectory_; };

private:
const boost::filesystem::path workingDirectory_;
};

} // namespace compiler_gym

0 comments on commit 3363c2d

Please sign in to comment.