Skip to content

Commit

Permalink
Add python bindings for detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanHenson committed Oct 6, 2023
1 parent cf9b73f commit 91bbf0a
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 2 deletions.
14 changes: 14 additions & 0 deletions awscrt/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,17 @@ def get_cpu_count_for_group(group_idx: int) -> int:
Returns number of processors in a given group.
"""
return _awscrt.get_cpu_count_for_group(group_idx)

class SystemEnvironment:

def __init__(self):
self._env = _awscrt.load_system_environment()

def is_ec2_nitro_instance(self) -> bool:
return _awscrt.is_env_ec2(self._env)

def get_ec2_instance_type(self) -> str:
return _awscrt.get_ec2_instance_type(self._env)

def is_crt_s3_optimized_for_ec2_instance_type(self) -> bool:
return _awscrt.is_crt_s3_optimized_for_ec2_instance_type(self._env)
91 changes: 91 additions & 0 deletions source/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#include <aws/common/system_info.h>
#include <aws/common/thread.h>
#include <aws/s3/s3.h>

const char *s_capsule_name_sys_env = "aws_system_environment";

PyObject *aws_py_get_cpu_group_count(PyObject *self, PyObject *args) {
(void)self;
Expand All @@ -26,6 +29,94 @@ PyObject *aws_py_get_cpu_count_for_group(PyObject *self, PyObject *args) {
return PyLong_FromSize_t(count);
}

static void s_sys_env_destructor(PyObject *sys_env_capsule) {
assert(PyCapsule_CheckExact(sys_env_capsule));

struct aws_system_environment *env = PyCapsule_GetPointer(sys_env_capsule, s_capsule_name_sys_env);
assert(env);

aws_system_environment_destroy(env);
}

PyObject *aws_py_load_system_environment(PyObject *self, PyObject *args) {
(void)self;
(void)args;

struct aws_allocator *allocator = aws_py_get_allocator();

struct aws_system_environment *env = aws_system_environment_load(allocator);

if (!env) {
return PyErr_AwsLastError();
}

PyObject *capsule = PyCapsule_New(env, s_capsule_name_sys_env, s_sys_env_destructor);

if (capsule == NULL) {
aws_system_environment_destroy(env);
return NULL;
}

return capsule;
}

PyObject *aws_py_is_env_ec2(PyObject *self, PyObject *args) {
PyObject *env_capsule = NULL;
if (!PyArg_ParseTuple(args, "O", &env_capsule)) {
return PyErr_AwsLastError();
}

struct aws_system_environment *env = PyCapsule_GetPointer(env_capsule, s_capsule_name_sys_env);
if (!env) {
return PyErr_AwsLastError();
}

struct aws_byte_cursor system_virt_name = aws_system_environment_get_virtualization_vendor(env);

if (aws_byte_cursor_eq_c_str_ignore_case(&system_virt_name, "amazon ec2")) {
Py_RETURN_TRUE;
}

Py_RETURN_FALSE;
}

PyObject *aws_py_get_ec2_instance_type(PyObject *self, PyObject *args) {
PyObject *env_capsule = NULL;
if (!PyArg_ParseTuple(args, "O", &env_capsule)) {
return PyErr_AwsLastError();
}

struct aws_system_environment *env = PyCapsule_GetPointer(env_capsule, s_capsule_name_sys_env);
if (!env) {
return PyErr_AwsLastError();
}

struct aws_byte_cursor product_name = aws_system_environment_get_virtualization_product_name(env);

return PyBytes_FromStringAndSize((const char *)product_name.ptr, product_name.len);
}

PyObject *aws_py_is_crt_s3_optimized_for_ec2_instance_type(PyObject *self, PyObject *args) {
PyObject *env_capsule = NULL;
if (!PyArg_ParseTuple(args, "O", &env_capsule)) {
return PyErr_AwsLastError();
}

struct aws_system_environment *env = PyCapsule_GetPointer(env_capsule, s_capsule_name_sys_env);
if (!env) {
return PyErr_AwsLastError();
}

bool is_optimized = aws_s3_is_optimized_for_system_env(env);

if (is_optimized) {
Py_RETURN_TRUE;
}

Py_RETURN_FALSE;
}


PyObject *aws_py_thread_join_all_managed(PyObject *self, PyObject *args) {
(void)self;

Expand Down
4 changes: 4 additions & 0 deletions source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ PyObject *aws_py_get_cpu_group_count(PyObject *self, PyObject *args);
PyObject *aws_py_get_cpu_count_for_group(PyObject *self, PyObject *args);

PyObject *aws_py_thread_join_all_managed(PyObject *self, PyObject *args);
PyObject *aws_py_load_system_environment(PyObject *self, PyObject *args);
PyObject *aws_py_is_env_ec2(PyObject *self, PyObject *args);
PyObject *aws_py_get_ec2_instance_type(PyObject *self, PyObject *args);
PyObject *aws_py_is_crt_s3_optimized_for_ec2_instance_type(PyObject *self, PyObject *args);

#endif /* AWS_CRT_PYTHON_COMMON_H */
6 changes: 6 additions & 0 deletions source/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,12 @@ static PyMethodDef s_module_methods[] = {
AWS_PY_METHOD_DEF(native_memory_usage, METH_NOARGS),
AWS_PY_METHOD_DEF(native_memory_dump, METH_NOARGS),
AWS_PY_METHOD_DEF(thread_join_all_managed, METH_VARARGS),
AWS_PY_METHOD_DEF(load_system_environment, METH_NOARGS),
AWS_PY_METHOD_DEF(is_env_ec2, METH_VARARGS),
AWS_PY_METHOD_DEF(get_ec2_instance_type, METH_VARARGS),
AWS_PY_METHOD_DEF(is_crt_s3_optimized_for_ec2_instance_type, METH_VARARGS),



/* IO */
AWS_PY_METHOD_DEF(is_alpn_available, METH_NOARGS),
Expand Down

0 comments on commit 91bbf0a

Please sign in to comment.