← Back to the chapter · Next detail →

Technical detail · Edge Cyber-Foraging: Cost and Energy Calculations

Discover and profile a surrogate

The discovery sketch records the address, port and advertised capabilities of _cyberforage._tcp.local.. Its first matching candidate is only a starting point: trust, current load and measured link conditions still need checking.

# Example: mDNS-based surrogate discovery
import zeroconf

class SurrogateDiscovery:
    def __init__(self):
        self.zeroconf = zeroconf.Zeroconf()
        self.surrogates = {}

    def on_service_state_change(self, name, state):
        if state == "added":
            info = self.zeroconf.get_service_info(
                "_cyberforage._tcp.local.", name
            )
            self.surrogates[name] = {
                "address": info.parsed_addresses()[0],
                "port": info.port,
                "capabilities": info.properties,
            }

    def get_best_surrogate(self, task_requirements):
        """Select surrogate matching task needs."""
        for name, surrogate in self.surrogates.items():
            if self.matches_requirements(surrogate, task_requirements):
                return surrogate
        return None

Task profiling.

Effective offloading requires understanding task characteristics:

# Example: Task profiler for offloading decisions
class TaskProfile:
    def __init__(self, task):
        self.cpu_intensity = self.estimate_cpu(task)       # in cycles
        self.memory_requirement = self.estimate_memory(task)  # in bytes
        self.input_size = len(task.input_data)               # in bytes
        self.expected_output_size = self.estimate_output(task)
        self.deadline_ms = task.deadline

    def should_offload(self, local_cpu, surrogate_cpu,
                       network_bandwidth, network_latency):
        """Decide whether offloading benefits this task."""
        # CPU rates are cycles/s, bandwidth is B/s, and latency is seconds.
        local_time = self.cpu_intensity / local_cpu
        transfer_time = (
            (self.input_size + self.expected_output_size)
            / network_bandwidth
        )
        remote_time = (
            self.cpu_intensity / surrogate_cpu
            + transfer_time
            + network_latency * 2  # round-trip overhead
        )
        return remote_time < local_time and remote_time * 1000 < self.deadline_ms

The profiler uses CPU cycles, bytes, cycles/s, B/s and seconds, then converts the result to milliseconds for the deadline check. Its doubled latency term assumes a one-way latency input. It checks speed and deadline only; battery and privacy require the policy described in the main text.

← Back to the chapter · Next detail →