Skip to content

Commit

Permalink
Implement search refining that can look for value increases/decreases
Browse files Browse the repository at this point in the history
  • Loading branch information
Toasterbirb committed Feb 1, 2025
1 parent 09964b2 commit b41fd24
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 17 deletions.
26 changes: 25 additions & 1 deletion include/Memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,27 @@ namespace harava
u8 bytes[8];
};

enum class change_type : u8
{
// equality types are given a numerical value
// so that they can be used directly with boolean
// comparisons
not_equal = 0,
equal = 1,
increased,
decreased,
changed_by_amount,
};

struct change_info
{
change_info(const change_type t, const std::string change = "0")
:type(t), amount(change) {}

change_type type;
type_bundle amount;
};

struct result
{
type_union value;
Expand All @@ -92,6 +113,9 @@ namespace harava

__attribute__((hot))
bool compare_bytes(const std::vector<u8>& bytes) const noexcept;

__attribute__((hot))
type_union compare_bytes_change(const std::vector<u8>& bytes) const noexcept;
};

struct results
Expand Down Expand Up @@ -145,7 +169,7 @@ namespace harava
results refine_search(const type_bundle new_value, results& old_results, const comparison comparison);

__attribute__((warn_unused_result))
results refine_search_change(results& old_results, const bool expected_result);
results refine_search_change(results& old_results, const change_info expected_result);

void set(result& result, const type_bundle value);
u64 region_count() const;
Expand Down
161 changes: 151 additions & 10 deletions src/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,39 @@ namespace harava
return match;
}

type_union result::compare_bytes_change(const std::vector<u8>& bytes) const noexcept
{
const u8 type_size = static_cast<u8>(type) & 0x0F;
type_union result;

// copy the bytes over
for (u8 i = 0; i < type_size; ++i)
result.bytes[i] = bytes[i + location];

// figure out the difference between the two values with subtraction
// the calling function is responsible for interpreting the results
switch (type)
{
case datatype::INT:
result._int -= value._int;
break;

case datatype::LONG:
result._long -= value._long;
break;

case datatype::FLOAT:
result._float -= value._float;
break;

case datatype::DOUBLE:
result._double -= value._double;
break;
}

return result;
}

u64 results::total_size() const
{
return int_results.size() * sizeof(i32)
Expand Down Expand Up @@ -433,7 +466,7 @@ namespace harava
return new_results;
}

results memory::refine_search_change(results& old_results, const bool expected_result)
results memory::refine_search_change(results& old_results, const change_info expected_result)
{
// expected_result == true (value unchanged)
// expected_result == false (value changed)
Expand All @@ -446,18 +479,126 @@ namespace harava

std::cout << "processing bytes\n";

std::for_each(std::execution::par_unseq, old_res_vec_ptrs.begin(), old_res_vec_ptrs.end(),
[&](const std::pair<u8, std::vector<result>*> res_vec)
if (expected_result.type == change_type::equal || expected_result.type == change_type::not_equal)
{
std::for_each(std::execution::par_unseq, old_res_vec_ptrs.begin(), old_res_vec_ptrs.end(),
[&](const std::pair<u8, std::vector<result>*> res_vec)
{
const auto& [vec_index, vec] = res_vec;
for (result r : *vec)
{
assert(region_cache.contains(r.region_id));
assert(region_cache.at(r.region_id).bytes.size() >= 8);
if (r.compare_bytes(region_cache.at(r.region_id).bytes) == static_cast<u8>(expected_result.type))
new_res_vec_ptrs.at(vec_index).second->emplace_back(r);
}
});
}

const auto process_bytes_inc_dec = [&old_res_vec_ptrs, &region_cache, &new_res_vec_ptrs, &expected_result](auto&& comparison_func)
{
std::for_each(std::execution::par_unseq, old_res_vec_ptrs.begin(), old_res_vec_ptrs.end(),
[&](const std::pair<u8, std::vector<result>*> res_vec)
{
const auto& [vec_index, vec] = res_vec;
for (result r : *vec)
{
assert(region_cache.contains(r.region_id));
assert(region_cache.at(r.region_id).bytes.size() >= 8);
type_union comparison_result = r.compare_bytes_change(region_cache.at(r.region_id).bytes);

if (comparison_func(comparison_result, r.type, expected_result))
new_res_vec_ptrs.at(vec_index).second->emplace_back(r);
}
});
};

if (expected_result.type == change_type::increased)
{
const auto cmp = [](const type_union result, const datatype type, const change_info info) -> bool
{
const auto& [vec_index, vec] = res_vec;
for (result r : *vec)
switch (type)
{
assert(region_cache.contains(r.region_id));
assert(region_cache.at(r.region_id).bytes.size() >= 8);
if (r.compare_bytes(region_cache.at(r.region_id).bytes) == expected_result)
new_res_vec_ptrs.at(vec_index).second->emplace_back(r);
case datatype::INT:
return result._int > 0;
break;

case datatype::LONG:
return result._long > 0;
break;

case datatype::FLOAT:
return result._float > 0;
break;

case datatype::DOUBLE:
return result._double > 0;
break;
}
});

return false;
};

process_bytes_inc_dec(cmp);
}

if (expected_result.type == change_type::decreased)
{
const auto cmp = [](const type_union result, const datatype type, const change_info info) -> bool
{
switch (type)
{
case datatype::INT:
return result._int < 0;
break;

case datatype::LONG:
return result._long < 0;
break;

case datatype::FLOAT:
return result._float < 0;
break;

case datatype::DOUBLE:
return result._double < 0;
break;
}

return false;
};

process_bytes_inc_dec(cmp);
}

if (expected_result.type == change_type::changed_by_amount)
{
const auto cmp = [](const type_union result, const datatype type, const change_info info) -> bool
{
switch (type)
{
case datatype::INT:
return result._int == info.amount._int;
break;

case datatype::LONG:
return result._long == info.amount._long;
break;

case datatype::FLOAT:
return result._float == info.amount._float;
break;

case datatype::DOUBLE:
return result._double == info.amount._double;
break;
}

return false;
};

process_bytes_inc_dec(cmp);
}

return new_results;
}
Expand Down
66 changes: 60 additions & 6 deletions src/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ namespace harava
}

harava::scope_timer timer(scan_duration_str);
results = process_memory->refine_search_change(results, true);
results = process_memory->refine_search_change(results, change_info(change_type::equal));
print_result_count();
}
},
Expand All @@ -246,7 +246,61 @@ namespace harava
}

harava::scope_timer timer(scan_duration_str);
results = process_memory->refine_search_change(results, false);
results = process_memory->refine_search_change(results, change_info(change_type::not_equal));
print_result_count();
}
},
{
"!",
"[value]",
"find values that have changed by X amount since last scan",
1,
[&]
{
if (first_search)
{
std::cout << do_initial_search_notif_str << '\n';
return;
}

harava::scope_timer timer(scan_duration_str);
results = process_memory->refine_search_change(results, change_info(change_type::changed_by_amount, command.args.at(0)));
print_result_count();
}
},
{
">",
"",
"find values that have increased since last scan",
0,
[&]
{
if (first_search)
{
std::cout << do_initial_search_notif_str << '\n';
return;
}

harava::scope_timer timer(scan_duration_str);
results = process_memory->refine_search_change(results, change_info(change_type::increased));
print_result_count();
}
},
{
"<",
"",
"find values that have decreased since last scan",
0,
[&]
{
if (first_search)
{
std::cout << do_initial_search_notif_str << '\n';
return;
}

harava::scope_timer timer(scan_duration_str);
results = process_memory->refine_search_change(results, change_info(change_type::decreased));
print_result_count();
}
},
Expand Down Expand Up @@ -289,11 +343,11 @@ namespace harava
switch (comparison)
{
case '!':
results = process_memory->refine_search_change(results, false);
results = process_memory->refine_search_change(results, change_info(change_type::not_equal));
break;

case '=':
results = process_memory->refine_search_change(results, true);
results = process_memory->refine_search_change(results, change_info(change_type::equal));
break;

default:
Expand Down Expand Up @@ -341,11 +395,11 @@ namespace harava
switch (comparison)
{
case '!':
results = process_memory->refine_search_change(results, false);
results = process_memory->refine_search_change(results, change_info(change_type::not_equal));
break;

case '=':
results = process_memory->refine_search_change(results, true);
results = process_memory->refine_search_change(results, change_info(change_type::equal));
break;

default:
Expand Down

0 comments on commit b41fd24

Please sign in to comment.