Files
rocm-systems/source/lib/rocprofiler-sdk/page_migration/utils.hpp
T
Mythreya 363f85dc72 Report page migration events as start/end (#793)
* Squashed commit of the following:

commit b76f2635f4b65599f03812a73d0cf410f5ada213
Author: Mythreya <mythreya.kuricheti@amd.com>
Date:   Fri Apr 26 00:29:09 2024 +0000

    Changed for PR feedback

commit bedb8ad566ff42fbf117b19202c26c507abcf8ac
Author: Jonathan R. Madsen <jonathanrmadsen@gmail.com>
Date:   Thu Apr 25 19:20:06 2024 -0500

    Fix installation

commit a98f8a69459a1450a1be9c98e20b3c1e7f2568c2
Author: Jonathan R. Madsen <jonathanrmadsen@gmail.com>
Date:   Thu Apr 25 19:16:35 2024 -0500

    Restructure the headers

commit 46489a020ffafdd5f4ce3f580469ff233ef67fe1
Author: Mythreya <mythreya.kuricheti@amd.com>
Date:   Tue Apr 23 23:31:10 2024 +0000

    Update hsa include

commit 8e795282cce348fc6aa736b7857b21aeb32aa20a
Author: Mythreya <mythreya.kuricheti@amd.com>
Date:   Tue Apr 23 23:02:32 2024 +0000

    Report page migration events as start/end

    * Updated tests accordingly
    * Page migration events are reported independently

commit 8784e5ad4895a626a2a8e4ac12f8021b34172bd4
Author: Mythreya <mythreya.kuricheti@amd.com>
Date:   Tue Apr 16 17:01:57 2024 +0000

    Update handling of dropped page migration events

    Previously, we dropped all locally buffered events when we detect that
    KFD has dropped some events. This may drop too many pending events too eagerly.

    When we receive an end event and cannot find the corresponding start,
    we can be sure that KFD has dropped some events in the immediate past.

    When this happens, we look through all locally buffered events and report
    the start events that are older than 10s as partial events --- they have
    no "end" information (we expect that the end events have been dropped).

    We also set the polling timeout to 10s to prevent the local buffer from
    getting too large with events waiting to be paired up.

    Updated tests

commit 2e8e0b07eeda9b5990e1ae8d28dcd3a035ce38e1
Author: Mythreya <mythreya.kuricheti@amd.com>
Date:   Tue Apr 16 17:01:31 2024 +0000

    Docs for triggers

* Fix page migration sample

* Fix hasher, kfd install

* Add hsa include
* Install KFD include dir

* Updates from code review

- single timestamp field
- node_id -> agent_id
- from_node -> from_agent
- to_node -> to_agent

* Misc revisions

* Remove page-migration install target

* Update page-migration pytest

* Tweak to serialization

* Address PR comments

* Update page-migration test

* Add cli args, update iterations

* Address PR comments

* Add abi.cpp for static_asserts
* Update page_migration gtest with only runtime tests
* Moved helpers into utils.hpp

---------

Co-authored-by: Jonathan R. Madsen <jonathanrmadsen@gmail.com>
2024-11-11 11:08:47 -06:00

198 lines
4.7 KiB
C++

// MIT License
//
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
#include "lib/common/container/small_vector.hpp"
#include "lib/rocprofiler-sdk/details/kfd_ioctl.h"
#include <rocprofiler-sdk/buffer_tracing.h>
#include <rocprofiler-sdk/fwd.h>
#include <algorithm>
#include <cstdint>
#include <string_view>
#include <unordered_map>
#include <utility>
namespace rocprofiler
{
namespace page_migration
{
/* serves as an overview of what events we capture and report
struct event_page_fault_start_t
{
int kind;
uint64_t timestamp;
int pid;
int node_id;
uint64_t address;
fault_t fault;
};
struct event_page_fault_end_t
{
int kind;
uint64_t timestamp;
uint32_t pid;
int node_id;
uint64_t address;
bool migrated;
};
struct event_migrate_start_t
{
int kind;
uint64_t timestamp;
uint32_t pid;
uint64_t start;
uint64_t end_offset;
uint32_t from;
uint32_t to;
uint32_t prefetch_node; // last prefetch location, 0 for CPU, or GPU id
uint32_t preferred_node; // perferred location, 0 for CPU, or GPU id
uint32_t trigger;
};
struct event_migrate_end_t
{
int kind;
uint64_t timestamp;
uint32_t pid;
uint64_t start;
uint64_t end_offset;
uint32_t from;
uint32_t to;
uint32_t trigger;
};
struct event_queue_eviction_t
{
int kind;
uint64_t timestamp;
uint32_t pid;
int node_id;
uint32_t trigger;
};
struct event_queue_restore_t
{
int kind;
uint64_t timestamp;
uint32_t pid;
int node_id;
bool rescheduled;
};
struct event_unmap_from_gpu_t
{
int kind;
uint64_t timestamp;
uint32_t pid;
uint64_t address;
uint64_t size;
int node_id;
uint32_t trigger;
};
*/
template <size_t>
struct page_migration_info;
using namespace rocprofiler::common;
namespace kfd
{
template <typename T>
struct IOC_event;
} // namespace kfd
constexpr size_t
bitmask(size_t num)
{
if(num == 0)
return 0;
else
return (1ULL << (num - 1));
}
template <size_t... Args>
constexpr size_t bitmask(std::index_sequence<Args...>)
{
return (bitmask(Args) | ...);
}
template <size_t... Ints>
constexpr size_t kfd_bitmask(std::index_sequence<Ints...>)
{
return (page_migration_info<Ints>::kfd_bitmask | ...);
}
template <size_t OpInx, size_t... OpInxs>
constexpr size_t
kfd_bitmask_impl(size_t rocprof_op, std::index_sequence<OpInx, OpInxs...>)
{
if(rocprof_op == OpInx) return page_migration_info<OpInx>::kfd_bitmask;
if constexpr(sizeof...(OpInxs) > 0)
return kfd_bitmask_impl(rocprof_op, std::index_sequence<OpInxs...>{});
else
return 0;
}
template <size_t... OpInxs>
constexpr auto
kfd_bitmask(const container::small_vector<size_t>& rocprof_event_ids,
std::index_sequence<OpInxs...>)
{
uint64_t m{};
for(const size_t& event_id : rocprof_event_ids)
{
m |= kfd_bitmask_impl(event_id, std::index_sequence<OpInxs...>{});
}
return m;
}
template <size_t OpInx, size_t... OpInxs>
constexpr size_t
kfd_to_rocprof_op(size_t kfd_id, std::index_sequence<OpInx, OpInxs...>)
{
if(kfd_id == page_migration_info<OpInx>::kfd_operation) return OpInx;
if constexpr(sizeof...(OpInxs) > 0)
return kfd_to_rocprof_op(kfd_id, std::index_sequence<OpInxs...>{});
else
return 0;
}
size_t
get_rocprof_op(const std::string_view event_data);
void
kfd_readlines(const std::string_view str, void(handler)(std::string_view));
using rocprof_buffer_op_t = rocprofiler_page_migration_operation_t;
using node_fd_t = int;
} // namespace page_migration
} // namespace rocprofiler