Skip to content

Commit

Permalink
Add block queue rotational parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrugman committed Dec 14, 2023
1 parent 0e9ab6e commit 1016989
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/pfs/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define PFS_BLOCK_HPP

#include "types.hpp"
#include "block_queue.hpp"

namespace pfs {

Expand All @@ -40,6 +41,7 @@ class block final
dev_t get_dev() const;

block_stat get_stat() const;
block_queue get_queue() const;

public: // Getters

Expand Down
56 changes: 56 additions & 0 deletions include/pfs/block_queue.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2020-present Daniel Trugman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef PFS_BLOCK_QUEUE_HPP
#define PFS_BLOCK_QUEUE_HPP

#include "types.hpp"

namespace pfs {

// Hint: See 'https://www.kernel.org/doc/Documentation/block/queue-sysfs.txt'
class block_queue final
{
public:
block_queue(const block_queue&) = default;
block_queue(block_queue&&) = default;

block_queue& operator=(const block_queue&) = delete;
block_queue& operator=(block_queue&&) = delete;

public: // Properties
const std::string& dir() const;

public: // Getters
bool get_rotational() const;

private:
friend class block;
block_queue(const std::string& block_root);

private:
static std::string build_block_queue_root(const std::string& block_root);

private:
static const std::string QUEUE_DIR;

private:
const std::string _block_queue_root;
};

} // namespace pfs

#endif // PFS_BLOCK_HPP
3 changes: 3 additions & 0 deletions sample/enum_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ static void enum_block(const pfs::block& block)

auto stat = block.get_stat();
print(stat);

auto queue = block.get_queue();
print(queue);
}
catch (const std::runtime_error& ex)
{
Expand Down
10 changes: 10 additions & 0 deletions sample/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <sstream>

#include "pfs/procfs.hpp"
#include "pfs/sysfs.hpp"

template <typename T>
inline std::string join(const T& container)
Expand Down Expand Up @@ -748,3 +749,12 @@ inline std::ostream& operator<<(std::ostream& out,

return out;
}

inline std::ostream& operator<<(std::ostream& out,
const pfs::block_queue& queue)
{
out << std::boolalpha;
out << "rotational[" << queue.get_rotational() << "] ";

return out;
}
5 changes: 5 additions & 0 deletions src/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ block_stat block::get_stat() const
return parsers::parse_block_stat_line(line);
}

block_queue block::get_queue() const
{
return block_queue(_block_root);
}

} // namespace pfs
56 changes: 56 additions & 0 deletions src/block_queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2020-present Daniel Trugman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <string>

#include "pfs/defer.hpp"
#include "pfs/parsers/common.hpp"
#include "pfs/parsers/generic.hpp"
#include "pfs/block_queue.hpp"

namespace pfs {

using namespace impl;

const std::string block_queue::QUEUE_DIR("queue/");

block_queue::block_queue(const std::string& block_root)
: _block_queue_root(build_block_queue_root(block_root))
{}

std::string block_queue::build_block_queue_root(const std::string& block_root)
{
return block_root + QUEUE_DIR;
}

const std::string& block_queue::dir() const
{
return _block_queue_root;
}

bool block_queue::get_rotational() const
{
static const std::string SIZE_FILE("rotational");
auto path = _block_queue_root + SIZE_FILE;

auto value = utils::readline(path);

int number;
parsers::to_number(value, number, utils::base::decimal);
return number != 0;
}

} // namespace pfs

0 comments on commit 1016989

Please sign in to comment.