Skip to content

Commit 7d0de4f

Browse files
authored
Run clang-format on c10d (#7791)
1 parent 42134ee commit 7d0de4f

File tree

10 files changed

+206
-232
lines changed

10 files changed

+206
-232
lines changed

torch/lib/c10d/FileStore.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#include "FileStore.hpp"
22

33
#include <assert.h>
4+
#include <fcntl.h>
45
#include <stdint.h>
56
#include <sys/file.h>
6-
#include <fcntl.h>
7-
#include <unistd.h>
87
#include <sys/stat.h>
8+
#include <unistd.h>
99

1010
#include <chrono>
1111
#include <functional>
@@ -15,19 +15,16 @@
1515
#include <system_error>
1616
#include <thread>
1717

18-
#define SYSASSERT(rv, ...) \
19-
if ((rv) < 0) { \
20-
throw std::system_error( \
21-
errno, \
22-
std::system_category(), \
23-
##__VA_ARGS__); \
18+
#define SYSASSERT(rv, ...) \
19+
if ((rv) < 0) { \
20+
throw std::system_error(errno, std::system_category(), ##__VA_ARGS__); \
2421
}
2522

2623
namespace c10d {
2724

2825
namespace {
2926

30-
template<typename F>
27+
template <typename F>
3128
typename std::result_of<F()>::type syscall(F fn) {
3229
while (true) {
3330
auto rv = fn();
@@ -121,7 +118,7 @@ class File {
121118
while (count > 0) {
122119
auto rv = syscall(std::bind(::write, fd_, buf, count));
123120
SYSASSERT(rv, "write");
124-
buf = (uint8_t*) buf + count;
121+
buf = (uint8_t*)buf + count;
125122
count -= rv;
126123
}
127124
}
@@ -130,7 +127,7 @@ class File {
130127
while (count > 0) {
131128
auto rv = syscall(std::bind(::read, fd_, buf, count));
132129
SYSASSERT(rv, "read");
133-
buf = (uint8_t*) buf + count;
130+
buf = (uint8_t*)buf + count;
134131
count -= rv;
135132
}
136133
}
@@ -189,14 +186,9 @@ off_t refresh(
189186

190187
} // namespace
191188

192-
FileStore::FileStore(const std::string& path)
193-
: Store(),
194-
path_(path),
195-
pos_(0) {
196-
}
189+
FileStore::FileStore(const std::string& path) : Store(), path_(path), pos_(0) {}
197190

198-
FileStore::~FileStore() {
199-
}
191+
FileStore::~FileStore() {}
200192

201193
void FileStore::set(const std::string& key, const std::vector<uint8_t>& value) {
202194
File file(path_, O_RDWR | O_CREAT);

torch/lib/c10d/FileStore.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ class FileStore : public Store {
1414

1515
virtual ~FileStore();
1616

17-
void set(
18-
const std::string& key,
19-
const std::vector<uint8_t>& value) override;
17+
void set(const std::string& key, const std::vector<uint8_t>& value) override;
2018

2119
std::vector<uint8_t> get(const std::string& key) override;
2220

torch/lib/c10d/Store.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ constexpr std::chrono::milliseconds Store::kDefaultTimeout;
66
constexpr std::chrono::milliseconds Store::kNoTimeout;
77

88
// Define destructor symbol for abstract base class.
9-
Store::~Store() {
10-
}
9+
Store::~Store() {}
1110

1211
} // namespace c10d

torch/lib/c10d/TCPStore.cpp

Lines changed: 52 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,25 @@
22

33
#include <poll.h>
44
#include <unistd.h>
5-
#include <system_error>
65
#include <algorithm>
6+
#include <system_error>
77

88
namespace c10d {
99

1010
namespace {
1111

12-
enum class QueryType : uint8_t {
13-
SET,
14-
GET,
15-
ADD,
16-
CHECK,
17-
WAIT
18-
};
12+
enum class QueryType : uint8_t { SET, GET, ADD, CHECK, WAIT };
1913

20-
enum class CheckResponseType : uint8_t {
21-
READY,
22-
NOT_READY
23-
};
14+
enum class CheckResponseType : uint8_t { READY, NOT_READY };
2415

25-
enum class WaitResponseType : uint8_t {
26-
STOP_WAITING
27-
};
16+
enum class WaitResponseType : uint8_t { STOP_WAITING };
2817

2918
} // anonymous namespace
3019

3120
// TCPStoreDaemon class methods
3221
// Simply start the daemon thread
33-
TCPStoreDaemon::TCPStoreDaemon(int storeListenSocket) :
34-
storeListenSocket_(storeListenSocket)
35-
{
22+
TCPStoreDaemon::TCPStoreDaemon(int storeListenSocket)
23+
: storeListenSocket_(storeListenSocket) {
3624
daemonThread_ = std::thread(&TCPStoreDaemon::run, this);
3725
}
3826

@@ -62,14 +50,15 @@ void TCPStoreDaemon::join() {
6250
void TCPStoreDaemon::run() {
6351
// Create the control pipe
6452
if (pipe(controlPipeFd_.data()) == -1) {
65-
throw std::runtime_error("Failed to create the control pipe to start the "
66-
"TCPStoreDaemon run");
53+
throw std::runtime_error(
54+
"Failed to create the control pipe to start the "
55+
"TCPStoreDaemon run");
6756
}
6857

6958
std::vector<struct pollfd> fds;
70-
fds.push_back({ .fd = storeListenSocket_, .events = POLLIN });
59+
fds.push_back({.fd = storeListenSocket_, .events = POLLIN});
7160
// Push the read end of the pipe to signal the stopping of the daemon run
72-
fds.push_back({ .fd = controlPipeFd_[0], .events = POLLHUP });
61+
fds.push_back({.fd = controlPipeFd_[0], .events = POLLHUP});
7362

7463
// receive the queries
7564
bool finished = false;
@@ -84,21 +73,25 @@ void TCPStoreDaemon::run() {
8473
// accept new connections.
8574
if (fds[0].revents != 0) {
8675
if (fds[0].revents ^ POLLIN) {
87-
throw std::system_error(ECONNABORTED, std::system_category(),
76+
throw std::system_error(
77+
ECONNABORTED,
78+
std::system_category(),
8879
"Unexpected poll revent on the master's listening socket: " +
89-
std::to_string(fds[0].revents));
80+
std::to_string(fds[0].revents));
9081
}
9182
int sockFd = std::get<0>(tcputil::accept(storeListenSocket_));
9283
sockets_.push_back(sockFd);
93-
fds.push_back({ .fd = sockFd, .events = POLLIN });
84+
fds.push_back({.fd = sockFd, .events = POLLIN});
9485
}
9586
// The pipe receives an event which tells us to shutdown the daemon
9687
if (fds[1].revents != 0) {
9788
// Will be POLLUP when the pipe is closed
9889
if (fds[1].revents ^ POLLHUP) {
99-
throw std::system_error(ECONNABORTED, std::system_category(),
90+
throw std::system_error(
91+
ECONNABORTED,
92+
std::system_category(),
10093
"Unexpected poll revent on the control pipe's reading fd: " +
101-
std::to_string(fds[1].revents));
94+
std::to_string(fds[1].revents));
10295
}
10396
finished = true;
10497
break;
@@ -112,10 +105,11 @@ void TCPStoreDaemon::run() {
112105
}
113106

114107
if (fds[fdIdx].revents ^ POLLIN) {
115-
throw std::system_error(ECONNABORTED, std::system_category(),
116-
"Unexpected poll revent: " +
117-
std::to_string(fds[fdIdx].revents) + " on socket: " +
118-
std::to_string(fds[fdIdx].fd));
108+
throw std::system_error(
109+
ECONNABORTED,
110+
std::system_category(),
111+
"Unexpected poll revent: " + std::to_string(fds[fdIdx].revents) +
112+
" on socket: " + std::to_string(fds[fdIdx].fd));
119113
}
120114
// Now query the socket that has the event
121115
try {
@@ -131,8 +125,8 @@ void TCPStoreDaemon::run() {
131125
::close(fds[fdIdx].fd);
132126

133127
// Remove all the tracking state of the close FD
134-
for (auto it = waitingSockets_.begin(); it != waitingSockets_.end(); ) {
135-
for (auto vecIt = it->second.begin(); vecIt != it->second.end(); ) {
128+
for (auto it = waitingSockets_.begin(); it != waitingSockets_.end();) {
129+
for (auto vecIt = it->second.begin(); vecIt != it->second.end();) {
136130
if (*vecIt == fds[fdIdx].fd) {
137131
vecIt = it->second.erase(vecIt);
138132
} else {
@@ -145,7 +139,7 @@ void TCPStoreDaemon::run() {
145139
++it;
146140
}
147141
}
148-
for (auto it = keysAwaited_.begin(); it != keysAwaited_.end(); ) {
142+
for (auto it = keysAwaited_.begin(); it != keysAwaited_.end();) {
149143
if (it->first == fds[fdIdx].fd) {
150144
it = keysAwaited_.erase(it);
151145
} else {
@@ -203,8 +197,8 @@ void TCPStoreDaemon::wakeupWaitingClients(const std::string& key) {
203197
if (socketsToWait != waitingSockets_.end()) {
204198
for (int socket : socketsToWait->second) {
205199
if (--keysAwaited_[socket] == 0) {
206-
tcputil::sendValue<WaitResponseType>(socket,
207-
WaitResponseType::STOP_WAITING);
200+
tcputil::sendValue<WaitResponseType>(
201+
socket, WaitResponseType::STOP_WAITING);
208202
}
209203
}
210204
waitingSockets_.erase(socketsToWait);
@@ -264,8 +258,8 @@ void TCPStoreDaemon::waitHandler(int socket) {
264258
keys[i] = tcputil::recvString(socket);
265259
}
266260
if (checkKeys(keys)) {
267-
tcputil::sendValue<WaitResponseType>(socket,
268-
WaitResponseType::STOP_WAITING);
261+
tcputil::sendValue<WaitResponseType>(
262+
socket, WaitResponseType::STOP_WAITING);
269263
} else {
270264
for (auto& key : keys) {
271265
waitingSockets_[key].push_back(socket);
@@ -275,27 +269,25 @@ void TCPStoreDaemon::waitHandler(int socket) {
275269
}
276270

277271
bool TCPStoreDaemon::checkKeys(const std::vector<std::string>& keys) const {
278-
return std::all_of(keys.begin(), keys.end(),
279-
[this](const std::string& s) {
280-
return tcpStore_.count(s) > 0;
281-
});
272+
return std::all_of(keys.begin(), keys.end(), [this](const std::string& s) {
273+
return tcpStore_.count(s) > 0;
274+
});
282275
}
283276

284277
// TCPStore class methods
285-
TCPStore::TCPStore(const std::string& masterAddr,
286-
PortType masterPort,
287-
bool isServer)
288-
: isServer_(isServer)
289-
, tcpStoreAddr_(masterAddr)
290-
, tcpStorePort_(masterPort)
291-
{
278+
TCPStore::TCPStore(
279+
const std::string& masterAddr,
280+
PortType masterPort,
281+
bool isServer)
282+
: isServer_(isServer),
283+
tcpStoreAddr_(masterAddr),
284+
tcpStorePort_(masterPort) {
292285
if (isServer_) {
293286
// Opening up the listening socket
294287
std::tie(masterListenSocket_, std::ignore) = tcputil::listen(masterPort);
295288
// Now start the daemon
296289
tcpStoreDaemon_ = std::unique_ptr<TCPStoreDaemon>(
297-
new TCPStoreDaemon(masterListenSocket_)
298-
);
290+
new TCPStoreDaemon(masterListenSocket_));
299291
}
300292
// Connect to the daemon
301293
storeSocket_ = tcputil::connect(tcpStoreAddr_, tcpStorePort_);
@@ -348,17 +340,19 @@ bool TCPStore::check(const std::vector<std::string>& keys) {
348340
}
349341
}
350342

351-
void TCPStore::wait(const std::vector<std::string>& keys,
352-
const std::chrono::milliseconds& timeout) {
343+
void TCPStore::wait(
344+
const std::vector<std::string>& keys,
345+
const std::chrono::milliseconds& timeout) {
353346
// Set the socket timeout if there is a wait timeout
354347
if (timeout != kNoTimeout) {
355348
struct timeval timeoutTV = {.tv_sec = timeout.count() / 1000,
356349
.tv_usec = (timeout.count() % 1000) * 1000};
357-
SYSCHECK(::setsockopt(storeSocket_,
358-
SOL_SOCKET,
359-
SO_RCVTIMEO,
360-
reinterpret_cast<char*>(&timeoutTV),
361-
sizeof(timeoutTV)));
350+
SYSCHECK(::setsockopt(
351+
storeSocket_,
352+
SOL_SOCKET,
353+
SO_RCVTIMEO,
354+
reinterpret_cast<char*>(&timeoutTV),
355+
sizeof(timeoutTV)));
362356
}
363357
tcputil::sendValue<QueryType>(storeSocket_, QueryType::WAIT);
364358
SizeType nkeys = keys.size();

torch/lib/c10d/TCPStore.hpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010
namespace c10d {
1111

1212
class TCPStoreDaemon {
13-
1413
public:
15-
1614
explicit TCPStoreDaemon(int storeListenSocket);
1715
~TCPStoreDaemon();
1816

1917
void join();
2018

2119
protected:
22-
2320
void run();
2421
void stop();
2522

@@ -32,7 +29,7 @@ class TCPStoreDaemon {
3229
void waitHandler(int socket);
3330

3431
bool checkKeys(const std::vector<std::string>& keys) const;
35-
void wakeupWaitingClients(const std::string &key);
32+
void wakeupWaitingClients(const std::string& key);
3633

3734
std::thread daemonThread_;
3835
std::unordered_map<std::string, std::vector<uint8_t>> tcpStore_;
@@ -43,22 +40,19 @@ class TCPStoreDaemon {
4340

4441
std::vector<int> sockets_;
4542
int storeListenSocket_;
46-
std::vector<int> controlPipeFd_ {-1, -1};
43+
std::vector<int> controlPipeFd_{-1, -1};
4744
};
4845

4946
class TCPStore : public Store {
50-
5147
public:
52-
53-
explicit TCPStore(const std::string& masterAddr,
54-
PortType masterPort,
55-
bool isServer = false);
48+
explicit TCPStore(
49+
const std::string& masterAddr,
50+
PortType masterPort,
51+
bool isServer = false);
5652

5753
virtual ~TCPStore();
5854

59-
void set(
60-
const std::string& key,
61-
const std::vector<uint8_t>& value) override;
55+
void set(const std::string& key, const std::vector<uint8_t>& value) override;
6256

6357
std::vector<uint8_t> get(const std::string& key) override;
6458

@@ -71,7 +65,6 @@ class TCPStore : public Store {
7165
const std::chrono::milliseconds& timeout = kDefaultTimeout) override;
7266

7367
protected:
74-
7568
bool isServer_;
7669
int storeSocket_ = -1;
7770
int masterListenSocket_ = -1;

0 commit comments

Comments
 (0)