Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging Optimizations #1714

Merged
merged 2 commits into from
Jun 25, 2022
Merged

Logging Optimizations #1714

merged 2 commits into from
Jun 25, 2022

Conversation

ryanmelt
Copy link
Contributor

Make all packet log writers share cycle thread. With large numbers of packets, this makes a huge difference in the number of threads needed to run COSMOS.

Default packet buffer to nil. This improves everywhere where a System is generated. Packets will not allocate space for their buffers until needed. Will greatly reduce RAM allocation in situations where there are large numbers of defined but unused packets.

Fix UDP multicast test on Mac. Macs do not like Multicast specifying port 0.

@ryanmelt ryanmelt requested a review from jmthomas June 25, 2022 14:38
@codecov
Copy link

codecov bot commented Jun 25, 2022

Codecov Report

Merging #1714 (09f3962) into master (69ef6a8) will decrease coverage by 0.05%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master    #1714      +/-   ##
==========================================
- Coverage   78.22%   78.17%   -0.06%     
==========================================
  Files         235      236       +1     
  Lines       18188    18251      +63     
==========================================
+ Hits        14228    14267      +39     
- Misses       3960     3984      +24     
Impacted Files Coverage Δ
...b/cosmos/tools/table_manager/table_manager_core.rb 88.46% <0.00%> (-10.00%) ⬇️
lib/cosmos/packets/structure.rb 95.79% <0.00%> (-2.32%) ⬇️
config/routes.rb 100.00% <0.00%> (ø)
app/controllers/completed_script_controller.rb 100.00% <0.00%> (ø)
app/models/table.rb 90.98% <0.00%> (ø)
lib/cosmos/logs/log_writer.rb 98.47% <0.00%> (+0.25%) ⬆️
lib/cosmos/tools/table_manager/table_config.rb 93.80% <0.00%> (+0.60%) ⬆️
app/controllers/application_controller.rb 100.00% <0.00%> (+11.11%) ⬆️

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 69ef6a8...09f3962. Read the comment docs.

@ryanmelt
Copy link
Contributor Author

The difference this makes is huge. My CPUs no longer have any stress.

Copy link
Contributor

@jmthomas jmthomas left a comment

Choose a reason for hiding this comment

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

Awesome improvement! Do you have a feel for the memory / CPU reductions?

@@ -196,23 +196,23 @@ static VALUE packet_initialize(int argc, VALUE *argv, VALUE self)
packet_name = argv[1];
default_endianness = symbol_BIG_ENDIAN;
description = Qnil;
buffer = rb_str_new2("");
buffer = Qnil;
Copy link
Contributor

Choose a reason for hiding this comment

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

Wow optimizations in the C code. Nice! Simply don't make the allocation up front ... you think this made a big difference too? Memory?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The buffer changes didn't make as much of a difference for the use cases we've been looking at because the packets aren't that big. If you had 50,000 large packets, it would make a significant difference.

return @buffer.length if @buffer

return 0
allocate_buffer()
Copy link
Contributor

Choose a reason for hiding this comment

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

This name is a little misleading and I deleted my previous comment. It's basically allocate buffer if needed. Maybe get_local_buffer() (I see you use the term local below).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll change to allocate_buffer_if_needed

def allocate_buffer
unless @buffer
@buffer = ZERO_STRING * @defined_length
@buffer.force_encoding(ASCII_8BIT_STRING)
Copy link
Contributor

Choose a reason for hiding this comment

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

Love encodings!

else
raise "No buffer available for method_missing"
return read(name.to_s)
Copy link
Contributor

Choose a reason for hiding this comment

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

So we always have @buffer now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Effectively. It will allocate if needed.

@@ -350,10 +350,10 @@ module Cosmos
end

describe "read_item", no_ext: true do
it "complains if no buffer given" do
it "works if no buffer given" do
Copy link
Contributor

Choose a reason for hiding this comment

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

Because a buffer is created internally right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

@ryanmelt
Copy link
Contributor Author

For the specific use case we've been looking at, it reduced the number of threads from more than the system could handle (100,000+) down to 110. CPU utilization seems to be about 75% less as well, but that is hard to really quantify as it couldn't handle it before.

@ryanmelt ryanmelt merged commit 406e47b into master Jun 25, 2022
@ryanmelt ryanmelt deleted the logging_optimizations branch June 25, 2022 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants