Skip to content

Worker: make Rust Worker the canonical Worker object#1376

Merged
sporksmith merged 2 commits intoshadow:devfrom
sporksmith:rust-worker
May 26, 2021
Merged

Worker: make Rust Worker the canonical Worker object#1376
sporksmith merged 2 commits intoshadow:devfrom
sporksmith:rust-worker

Conversation

@sporksmith
Copy link
Copy Markdown
Contributor

This is a step towards moving the object graph into Rust, so that we can
sort out ownership / sharing issues.

The Rust Worker object is currently just a thin wrapper around the
legacy C Worker object (now CWorker). It should be straightforward to
start moving more members out of the CWorker directly into the Worker,
but I'd like to prioritize getting all of the Shadow objects into at
least this state before converting any one object completely.

  • The pieces of the Worker that are accessed from outside the Worker are
    moved into parallel arrays in the WorkerPool. This allows the Worker
    itself to be stored in a thread-local without any thread-safe wrappers,
    for cheaper access.

  • I removed support for 0-Workers, since it was already planned, and
    it'd have further complicated the previous change.

@codecov
Copy link
Copy Markdown

codecov bot commented May 25, 2021

Codecov Report

Merging #1376 (ef48c1a) into dev (34db1e7) will increase coverage by 0.06%.
The diff coverage is 85.55%.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev    #1376      +/-   ##
==========================================
+ Coverage   54.20%   54.26%   +0.06%     
==========================================
  Files         137      137              
  Lines       20565    20557       -8     
  Branches     5187     5181       -6     
==========================================
+ Hits        11148    11156       +8     
+ Misses       6493     6481      -12     
+ Partials     2924     2920       -4     
Flag Coverage Δ
tests 54.26% <85.55%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/main/utility/fork_proxy.c 0.00% <0.00%> (ø)
src/main/core/worker.c 76.33% <85.02%> (+3.24%) ⬆️
src/main/core/logical_processor.c 84.12% <100.00%> (+1.07%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 34db1e7...ef48c1a. Read the comment docs.

@sporksmith sporksmith requested a review from stevenengler May 25, 2021 15:19
Comment on lines +456 to +496
void* _worker_run(void* voidWorkerThreadInfo) {
// Take contents of `info` and free it.
WorkerPool* workerPool = NULL;
int threadID = -1;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to have some comments in here about thread safety (and how each thread must only write to data in element threadID for the members of workerPool and not any other parts of workerPool).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done - added the comments to the struct definition itself and referenced them from here. It's a bit hairy now, but of course will be forced to be made more rigorous when WorkerPool itself is moved to Rust :)

Comment on lines +515 to +516
// Destroy the thread-local Worker.
worker_freeForThisThread();
Copy link
Copy Markdown
Contributor

@stevenengler stevenengler May 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that thread-locals get dropped when a (non-main) thread exits. Do we still need to do that here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Yeah, I had been thinking destroying it explicitly might save us in the case that the Worker destructors ended up relying on other thread-locals, which could have already been destroyed if we're in that phase. Probably better to ensure any such code is resilient to the thread-local no longer being available, though. (e.g. ShadowLogger should already handle this gracefully).

#include "main/core/support/definitions.h"
#include "main/host/protocol.h"
#include "main/routing/packet.h"
#include "support/logger/log_level.h"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the LogInfoFlags enum be moved to a separate file so that it's not before the include statements?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Oops, yeah meant to circle back and fix that.

This is a step towards moving the object graph into Rust, so that we can
sort out ownership / sharing issues.

The Rust Worker object is currently just a thin wrapper around the
legacy C Worker object (now CWorker). It should be straightforward to
start moving more members out of the CWorker directly into the Worker,
but I'd like to prioritize getting all of the Shadow objects into at
least this state before converting any one object completely.

* The pieces of the Worker that are accessed from outside the Worker are
moved into parallel arrays in the WorkerPool. This allows the Worker
itself to be stored in a thread-local without any thread-safe wrappers,
for cheaper access.

* I removed support for 0-Workers, since it was already planned, and
it'd have further complicated the previous change.
@sporksmith sporksmith merged commit c6ee456 into shadow:dev May 26, 2021
@sporksmith sporksmith deleted the rust-worker branch May 26, 2021 16:20
sporksmith added a commit to sporksmith/shadow that referenced this pull request May 26, 2021
Deprecation of support for 0-worker-threads was proposed in shadow#1251 and
initially implemented in shadow#1376. This cleans up some
no-longer-reachable-code.

In a similar spirit (that it's not worth supporting more code paths to
optimize a niche use-case), it also removes the special case of
not-shuffling the hosts when there is only 1 worker.
@robgjansen robgjansen added Component: Main Composing the core Shadow executable Type: Enhancement New functionality or improved design labels May 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Component: Main Composing the core Shadow executable Type: Enhancement New functionality or improved design

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants